[英]WPF: Get the item index touched in a stack panel
I have a StackPanel. 我有一个StackPanel。 The contents of the StackPanel are defined in a Data Template and they are basically another StackPanel composed of two Buttons and two Text Blocks.
StackPanel的内容在数据模板中定义,它们基本上是另一个由两个按钮和两个文本块组成的StackPanel。 Now... when I touch the StackPanel I can get the element I touched through
现在...当我触摸StackPanel时,我可以得到我触摸过的元素
e.TouchDevice.DirectlyOver
Where e is a TouchEventArgs variable. 其中e是TouchEventArgs变量。 But I can only get the actual UIElement... I'm intrested in getting the index of the element of the StackPanel that I touched.
但是我只能得到实际的UIElement ...我很想获得我接触过的StackPanel的元素的索引。
Anyone knows how to do that? 有人知道该怎么做吗?
Thank you. 谢谢。
XAML: XAML:
<DataTemplate x:Key="AddBookListTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9.9*"/>
<ColumnDefinition Width="0.1*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" >
<StackPanel Orientation="Horizontal">
<Button blabla>
</Button>
<Button blabla>
</Button>
</StackPanel>
<TextBlock/>
<TextBlock/>
</StackPanel>
</Grid>
</DataTemplate>
And is reused 并被重用
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Books}" Margin="0 10 0 0" Background="Transparent" ItemTemplate="{StaticResource AddBookListTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" TouchEnter="StackPanel_TouchEnter"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
You could simply use the IndexOf
method on the panel's Children
collection: 您可以简单地使用面板的
Children
集合上的IndexOf
方法:
private void panel_Touch(object sender, RoutedEventArgs e)
{
MessageBox.Show("You've touched n°" + panel.Children.IndexOf(sender as UIElement));
}
You could use VisualTreeHelper to walk up the visual tree, and then use IndexOf() to get the index: 您可以使用VisualTreeHelper在可视树上移动,然后使用IndexOf()获取索引:
private void panel_Touch(object sender, RoutedEventArgs e)
{
int index;
var button = sender as Button;
if (button != null)
{
var contentPresenter = VisualTreeHelper.GetParent(button) as ContentPresenter;
if (contentPresenter != null)
{
var stackPanel = VisualTreeHelper.GetParent(contentPresenter) as StackPanel;
if (stackPanel != null)
index = stackPanel.Children.IndexOf(contentPresenter);
}
}
}
Use a tool like Snoop to view the visual treee so you know what types to expect (I think that StackPanel puts ContentPresenters around each item, but I could be wrong). 使用诸如Snoop之类的工具查看可视树,这样您就可以知道预期的类型(我认为StackPanel将ContentPresenters放在每个项目周围,但是我可能是错的)。
It's a bit of a hack though. 不过这有点hack。 I'd recommend something more like this:
我建议更像这样的东西:
<StackPanel Orientation="Horizontal">
<Button Content="OK" Command="{Binding OKCommand}" />
<Button Content="Cancel" Command="{Binding CancelCommand}" />
</StackPanel>
Where OKCommand and CancelCommand are RelayCommands on your ViewModel . 其中OKCommand和CancelCommand是ViewModel上的RelayCommands 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.