Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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
C# 选定的WPF树视图项目未激发(和其他问题)_C#_Wpf_Treeview_Treeviewitem - Fatal编程技术网

C# 选定的WPF树视图项目未激发(和其他问题)

C# 选定的WPF树视图项目未激发(和其他问题),c#,wpf,treeview,treeviewitem,C#,Wpf,Treeview,Treeviewitem,我正在尝试构建一个文件系统的树状视图,节点是目录,叶子是文件 我有xaml来进行数据绑定,但是我无法触发TreeViewItem selected事件,或者我无法检测到它 <Window x:Class="List.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2

我正在尝试构建一个文件系统的树状视图,节点是目录,叶子是文件

我有xaml来进行数据绑定,但是我无法触发TreeViewItem selected事件,或者我无法检测到它

<Window x:Class="List.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:List"
        xmlns:dmodels="clr-namespace:List.DataModels"
        Title="MainWindow" Height="350" Width="525">

        <Window.Resources>
            <ResourceDictionary>
            <HierarchicalDataTemplate DataType="{x:Type dmodels:DirectoryNode}" ItemsSource="{Binding Children}">
                <TreeViewItem FontSize="16" FontWeight="Bold" Header="{Binding Path=DisplayName}" Selected="TVI_Selected" >
                        <TextBlock Text="Please Wait..." MouseDown="Listbox1_MouseLeftButtonDown"  />
                </TreeViewItem>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type dmodels:FileNode}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="20,0,0,0" Text="{Binding Path=DisplayName}" FontWeight="Bold" MouseDown="Listbox1_MouseLeftButtonDown"  />
                    </StackPanel>
            </DataTemplate>
        </ResourceDictionary>
        </Window.Resources>

    <Grid>
        <TreeView x:Name="myTree" ItemsSource="{Binding Files}" SelectedItemChanged="TreeSelectionChanged" >
        </TreeView>
    </Grid>
</Window>
我将“节点目录”显示为TreeViewItem,以便获得expander切换,但单击或双击时,不会调用TVI_Selected方法。单击或双击叶文件时,会调用TreeSelectionChanged方法,但它不是抑制expander切换的TreeViewItem

我想截获所选事件,以便替换“请稍候…”。。。使用适当的子数据

因为我是新手,我很有可能做了一些愚蠢的事情,或者做得很糟糕,或者就是不明白——如果有更好的方法来做这些事情,我很乐意听到

private void  TVI_Selected ( object sender, RoutedEventArgs e )
{
    Console.WriteLine ( " TreeViewItem selection Changed " );
}

private void TreeSelectionChanged ( object sender, RoutedPropertyChangedEventArgs<Object> e )
{
    //Perform actions when SelectedItem changes
    BaseNode node = e.NewValue as BaseNode;

    if (node != null)
    {
        string str = node.DisplayName;
        string s2  = (node.IsDirectory == true) ? "Directory" : "File";
        Console.WriteLine ( " tree selection = {0} is a {1}", str, s2 );
    }
}
我认为您的问题可能在于您的HierarchycalDataTemplate。您不应该在那里声明TreeView项,因为TreeView会自动将您在DataTemplate中定义的内容包装到TreeView项中。尝试删除它并在中定义子项:

有关更多信息,请参阅Mike Hillberg关于Wpf和Silverlight on MSDN的博客页面。

我认为您的问题可能在于您的HierarchicalDataTemplate。您不应该在那里声明TreeView项,因为TreeView会自动将您在DataTemplate中定义的内容包装到TreeView项中。尝试删除它并在中定义子项:


有关更多信息,请参阅Mike Hillberg关于Wpf和MSDN上Silverlight的博客中的页面。

谢谢,但我将TreeView项目放在HDT中,因为没有它,我无法显示+扩展切换。如果它真的被包装了,为什么TreeViewItem会存在?我不是想成为一个聪明人,我只是不明白这一点。我会看看你给的链接——我相信也是这样。谢谢可能+没有出现是因为您没有定义子项?TreeViewItem作为数据对象的“容器”存在,如ListBoxItem或MenuItem。我们可以将容器的样式和操作与数据内容分开。。。它非常有用。例如,容器具有非常有用的IsSelected属性,以便我们知道选择了哪个数据项。是-没有显示+,因为存在的子项是空的。现在我需要的“虚拟”物品相当笨重,是吗?。谢谢你,我正在慢慢进步。感谢您的耐心和帮助。谢谢,但我将TreeView项目放入HDT,因为没有它,我无法显示+扩展切换。如果它真的被包装了,为什么TreeViewItem会存在?我不是想成为一个聪明人,我只是不明白这一点。我会看看你给的链接——我相信也是这样。谢谢可能+没有出现是因为您没有定义子项?TreeViewItem作为数据对象的“容器”存在,如ListBoxItem或MenuItem。我们可以将容器的样式和操作与数据内容分开。。。它非常有用。例如,容器具有非常有用的IsSelected属性,以便我们知道选择了哪个数据项。是-没有显示+,因为存在的子项是空的。现在我需要的“虚拟”物品相当笨重,是吗?。谢谢你,我正在慢慢进步。我感谢你的耐心和帮助。
<HierarchicalDataTemplate DataType="{x:Type dmodels:DirectoryNode}"
    ItemsSource="{Binding Children}">
    <StackPanel>  <!-- Define parent items here-->
        <TextBlock FontSize="16" FontWeight="Bold" Text="{Binding Path=DisplayName}">
        <TextBlock Text="Please Wait..." MouseDown="Listbox1_MouseLeftButtonDown" />
    </StackPanel>
    <HierarchicalDataTemplate.ItemTemplate> 
        <DataTemplate>
            <TextBlock Text="{Binding Name}" /> <!-- Define child items here-->
        </DataTemplate> 
    </HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>