Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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
WPF操作树视图中的选定项_Wpf_Vb.net_Treeview_Selecteditem - Fatal编程技术网

WPF操作树视图中的选定项

WPF操作树视图中的选定项,wpf,vb.net,treeview,selecteditem,Wpf,Vb.net,Treeview,Selecteditem,我有一个树状视图,其中有可选择的项,以及可能触发某些代码操作的子项。单击子项时,应执行操作,但父项必须保留/成为选定项 我面临的问题是项目没有被正确取消选择,导致在树视图中选择多个项目 以下是treeview的xaml: <TreeView Name="Treeview1" Style="{StaticResource vcc_Treeview}" > <TreeView.ItemTemplate > <Hierarc

我有一个树状视图,其中有可选择的项,以及可能触发某些代码操作的子项。单击子项时,应执行操作,但父项必须保留/成为选定项

我面临的问题是项目没有被正确取消选择,导致在树视图中选择多个项目

以下是treeview的xaml:

    <TreeView Name="Treeview1" Style="{StaticResource vcc_Treeview}" >
        <TreeView.ItemTemplate >
            <HierarchicalDataTemplate ItemsSource="{Binding Children}" >
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImgSrc}" Style="{StaticResource vcc_TreeviewItemImage}" />
                    <TextBlock Text="{Binding Description}" Style="{StaticResource vcc_TreeviewItemTextblock}" Foreground="{Binding TextColorBrush}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
        <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay, Converter={StaticResource clsBindingDebugger}}" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>
最后,我触发事件MyTreeviewItem\u SelectedChanged:

Private ReselectingParent As Boolean = False

Private Sub MyTreeviewItem_SelectedChanged(MTI As MyTreeviewItem, IsSelected As Boolean)
Debug.Print(MTI.Description & "  Selected = " & IsSelected)
If ReselectingParent Then Exit Sub

If IsSelected Then

    'Do some (action) stuff here

    If MTI.IsActionItem AndAlso MTI.Parent IsNot Nothing Then
        ReselectingParent = True 'to prevent this sub from being executed with the next 2 lines
        MTI.IsSelected = False
        MTI.Parent.IsSelected = True
        ReselectingParent = False
    End If
End If
End Sub
假设树视图如下所示:

item 1
--Action 1
--Action 2
Item 2
--Action 3
--Action 4
在我开始的情况下,项目2被选中。现在,我单击操作1,因此应该执行该操作,然后项目1应该是所选项目。 debug.print将在以下行中显示结果:

ConvertBack: False
Item 2  Selected = False
Convert: False
ConvertBack: True
Action 2  Selected = True
Action 2  Selected = False
Convert: True
Item 1  Selected = True
Convert: False
    ReselectingParent = True 'to prevent this sub from being executed with the next 2 lines
    MTI.IsSelected = False
    MTI.Parent.IsSelected = True
    ReselectingParent = False
此时,应选择项目1。 现在我点击动作3

ConvertBack: False
Action 2  Selected = False
Convert: False
ConvertBack: True
Action 3  Selected = True
Action 3  Selected = False
Convert: True
Item 2  Selected = True
Convert: False
结果是项目1和项目2都被选中,其中只应选择项目2。第二个调试器块显示“Action 2 Selected=False”,这应该是“Item 1 Selected=False”

我希望我明白了。有人能告诉我解决问题的办法吗?
谢谢

对于感兴趣的人

我仍然不太明白为什么代码不能正常工作。但显然,单击的操作项没有被正确取消选择。不要打这些电话:

ConvertBack: False
Item 2  Selected = False
Convert: False
ConvertBack: True
Action 2  Selected = True
Action 2  Selected = False
Convert: True
Item 1  Selected = True
Convert: False
    ReselectingParent = True 'to prevent this sub from being executed with the next 2 lines
    MTI.IsSelected = False
    MTI.Parent.IsSelected = True
    ReselectingParent = False
直接在MyTreeviewItem\u Selected Changed中,我现在改为调用计时器。在timer_appeased事件中,我调用四行。 显然,在调用代码选择其父项时,操作项的主选择尚未完成。这是一个丑陋的解决办法,但对我来说很有效