简体   繁体   English

选择的WPF treeviewitem不会触发(和其他问题)

[英]WPF treeviewitem selected doesn't fire (and other issues)

I am (trying to) build a TreeView of the file system (Nodes are directories, leaves are files). 我(尝试)构建文件系统的TreeView(节点是目录,叶子是文件)。

I have the xaml to do the data binding, but I cannot get the TreeViewItem selected event to fire (or I cannot detect it). 我有xaml来做数据绑定,但是我无法触发TreeViewItem选择的事件(或者我无法检测到它)。

<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>

I have the 'Node (directory)' displayed as a TreeViewItem (so that I can get the expander toggle), but when it is either clicked, or double-clicked, the TVI_Selected method does not get called. 我将'节点(目录)'显示为TreeViewItem(以便我可以获得扩展器切换),但是当单击或双击时,TVI_Selected方法不会被调用。 When the Leaf (file)' is clicked, or double-clicked, the TreeSelectionChanged method DOES get called, but it is not a TreeViewItem (to suppress the expander toggle). 单击Leaf(文件)'或双击时,将调用TreeSelectionChanged方法,但它不是TreeViewItem(用于抑制扩展器切换)。

I want to intercept the Selected event so that I can replace the "Please Wait..." with the appropriate children data. 我想拦截Selected事件,以便我可以用适当的子数据替换“Please Wait ...”。

Since I am new to this, there is high probability that I am doing something stupid, or badly, or just don't get it -- if there is a better way to do any of this, I'd love to hear it. 由于我是新手,我很有可能做一些愚蠢或严重的事情,或者只是没有得到它 - 如果有更好的方法可以做到这一点,我很乐意听到它。

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 );
    }
}

I think that your problem might lie in your HierarchicalDataTemplate . 我认为您的问题可能在于HierarchicalDataTemplate You're not supposed to declare an TreeViewItem there as the TreeView will automatically wrap what you define in the DataTemplate in a TreeViewItem anyway. 您不应该在那里声明TreeViewItem ,因为TreeView会自动将您在DataTemplate中定义的内容包装在TreeViewItem Try removing it and defining the child items in the HierarchicalDataTemplate.ItemTemplate property : 尝试删除它并在HierarchicalDataTemplate.ItemTemplate属性中定义子项:

<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>

For more information, please see the TreeView and HierarchicalDataTemplate, Step-by-Step page from Mike Hillberg's Blog on Wpf and Silverlight on MSDN. 有关更多信息,请参阅Mike Hillberg的关于Wpf的博客和MSDN上的Silverlight的TreeView和HierarchicalDataTemplate,Step-by-Step页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM