Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过TreeViewItem对象访问WPF Treeview所选索引_Wpf_Treeview - Fatal编程技术网

通过TreeViewItem对象访问WPF Treeview所选索引

通过TreeViewItem对象访问WPF Treeview所选索引,wpf,treeview,Wpf,Treeview,我有一个使用DataTemplate的树状视图,每个项目都有复选框 <TreeView ItemsSource="{Binding}"> <DataTemplate DataType="{x:Type local:MatchDataLeaf}"> <Grid Margin="3"> <Grid.ColumnDefinitions> <ColumnDefinition Width="240"/&g

我有一个使用DataTemplate的树状视图,每个项目都有复选框

<TreeView ItemsSource="{Binding}">
<DataTemplate DataType="{x:Type local:MatchDataLeaf}">
    <Grid Margin="3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="240"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="60"/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0" Orientation="Horizontal">
            <CheckBox x:Name="selectCheckBtn" Grid.Column="0" IsChecked="True" Click="select_Click"
                      Tag="{Binding}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"/>
            <TextBlock Grid.Column="1" Margin="5,0,0,0" Text="{Binding Path=Name}" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="Black" VerticalAlignment="Center"/>
    </StackPanel>
 </Grid>
</DataTemplate>
但是这里s=-1


我在复选框上也有标记,其中包含底层对象。当然,我可以在列表中查找对象,但似乎必须有一种更简单的方法来查找索引。

要获取的Items控件可能是StackPanel或网格。您应该能够通过事件发送器访问复选框,并导航到TreeView项和TreeView,并使用IndexOf


谢谢sp的父项是网格,其父项为null。请尝试使用VisualTreeHelper.GetParentgrid.returns ContentPresenter-如何从中获取TreeItem?嗯,这有点混乱。我已经修改了我的答案。是的,也许看起来不太好,但很有效:-谢谢你的帮助。
ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(selectedItem);
int s = parent.Items.IndexOf(selectedItem);
 private void CheckBox_Click(object sender, RoutedEventArgs e)
 {
        CheckBox cb = (CheckBox)sender;
        StackPanel sp = (StackPanel)cb.Parent;
        Grid g = (Grid)sp.Parent;
        ContentPresenter cp = (ContentPresenter)VisualTreeHelper.GetParent(g);
        IList l = (IList)myTreeView.ItemsSource;
        object o = cp.Content;
        MessageBox.Show(l.IndexOf(o).ToString());
 }