问题描述
我有一个的ListView
只包含按钮。我想要做的是pretty简单,我想有一个被点击按钮的索引。列表的计数从0到100,因此,当用户点击按钮6,我需要这个号码进行处理。
我定义我的的ListView
是这样的:
< ListView控件名称=myListView
的ItemsSource ={绑定源= {StaticResource的myDataModel},
路径= StatusList,
模式=单向}>
< ListView.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=横向>< / StackPanel的>
< / ItemsPanelTemplate>
< /ListView.ItemsPanel> < ListView.ItemTemplate>
<&DataTemplate的GT;
<按钮模式=单向}
点击=Button_Click/>
< / DataTemplate中>
< /ListView.ItemTemplate>
< /&的ListView GT;
我最初的想法是创建一个带有ID的自定义按钮,并绑定指数的ID,但我无法弄清楚如何做到这一点。
我试过:
int类型的= myListView.Items.IndexOf(((按钮)发送方));
在事件处理中,但它总是返回为0xffffffff
有谁能够告诉我怎么去点击的按钮的索引?
这应该工作:
与一个ItemsControl交换的ListView和一个AlternationCount设置为非常高的数目(比清单中的最大计数元件更高)。
做一个命令,并通过当前指数作为参数。
XAML:
< Window.CommandBindings>
<的CommandBinding
命令=选择
执行=Click_Executed/>
< /Window.CommandBindings>< ItemsControl的AlternationCount =9999NAME =myListView
的ItemsSource ={绑定源= {StaticResource的myDataModel},
路径= StatusList,
模式=单向}>
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=横向>< / StackPanel的>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel> < ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<按钮命令=选择
CommandParameter ={绑定路径=(ItemsControl.AlternationIndex)的RelativeSource = {的RelativeSource TemplatedParent}}
CONTENT ={绑定的RelativeSource = {的RelativeSource模式= TemplatedParent},路径=(ItemsControl.AlternationIndex)}WIDTH =200HEIGHT =20点击=Button_Click/>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>
code背后:
私人无效Click_Executed(对象发件人,ExecutedRoutedEventArgs E)
{
MessageBox.Show(索引:+ e.Parameter.ToString());
}
I have a ListView
containing only buttons. What I want to do is pretty simple, I want to have the index of the button that has been clicked. The count of the list varies from 0 to 100, so when the user clicks on button 6, I need this number for processing.
I defined my ListView
like this:
<ListView Name="myListView"
ItemsSource="{Binding Source={StaticResource myDataModel},
Path=StatusList,
Mode=OneWay}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Button Mode=OneWay}"
Click="Button_Click"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
My original idea was to create a custom button with an ID and bind the index to the ID but I can't figure out how to do that.
I tried:
int a = myListView.Items.IndexOf(((Button)sender));
inside the event handler, but it always returns 0xffffffffcan anybody tell me how to get the index of the clicked button?
This should work:
Swap your ListView with an ItemsControl and set an AlternationCount to a very high number (higher than the max count elements in your list).Make a command and pass the current index as a parameter.
XAML:
<Window.CommandBindings>
<CommandBinding
Command="Select"
Executed="Click_Executed" />
</Window.CommandBindings>
<ItemsControl AlternationCount="9999" Name="myListView"
ItemsSource="{Binding Source={StaticResource myDataModel},
Path=StatusList,
Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="Select"
CommandParameter="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource TemplatedParent}}"
Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}" Width="200" Height="20" Click="Button_Click"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Code Behind:
private void Click_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Index: " + e.Parameter.ToString());
}
这篇关于在包含按钮一个ListView,如何获得点击的一个指数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!