WPF:在堆栈面板中获取项目索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23873416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:47:14  来源:igfitidea点击:

WPF: Get the item index touched in a stack panel

c#wpfstackpaneltouch-event

提问by Marco

I have a 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. Now... when I touch the StackPanel I can get the element I touched through

我有一个 StackPanel。StackPanel 的内容定义在一个数据模板中,它们基本上是另一个由两个按钮和两个文本块组成的 StackPanel。现在......当我触摸 StackPanel 时,我可以得到我触摸过的元素

e.TouchDevice.DirectlyOver

Where e is a TouchEventArgs variable. But I can only get the actual UIElement... I'm intrested in getting the index of the element of the StackPanel that I touched.

其中 e 是 TouchEventArgs 变量。但我只能获得实际的 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>

回答by Pragmateek

You could simply use the IndexOfmethod on the panel's Childrencollection:

您可以简单地使用IndexOf面板Children集合上的方法:

private void panel_Touch(object sender, RoutedEventArgs e)
{
    MessageBox.Show("You've touched n°" + panel.Children.IndexOf(sender as UIElement));
}

回答by MatrixManAtYrService

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 在每个项目周围放置了 ContentPresenter,但我可能是错的)。

It's a bit of a hack though. 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 RelayCommandson your ViewModel.

其中 OKCommand 和 CancelCommand 是ViewModel上的RelayCommands