WPF中一个比较完整的树形结构和右键菜单

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32493189/article/details/52974455

1.xaml中的代码

<UserControl x:Class="UserManagerMgr.Views.UserTreeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity" 
             xmlns:cal="http://www.caliburnproject.org"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <ResourceDictionary>

            <HierarchicalDataTemplate x:Key="UserTemplate" >
                <StackPanel Orientation="Horizontal">
                    <Border x:Name="br" VerticalAlignment="Center">
                        <Grid>
                             <Image x:Name="icon" Width="16" Height="16" Stretch="UniformToFill" Source=""/>
                            <TextBlock />
                        </Grid>
                    </Border>
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
                <HierarchicalDataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Authority}" Value="OPERATOR">
                        <Setter TargetName="icon" Property="Source" Value=""/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Authority}" Value="WATCHKEEPER">
                        <Setter TargetName="icon" Property="Source" Value=""/>
                    </DataTrigger>
                </HierarchicalDataTemplate.Triggers>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="GroupsTemplate" ItemsSource="{Binding Children}" ItemTemplate="{StaticResource UserTemplate}">
                <StackPanel Orientation="Horizontal">
                    <Border  x:Name="br" VerticalAlignment="Center">
                        <Image x:Name="icon" Width="16" Height="16" Stretch="UniformToFill" Source=""/>
                    </Border>
                    <TextBlock x:Name="txt"></TextBlock.Text>
                </StackPanel>                
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding Children}" ItemTemplate="{StaticResource GroupsTemplate}">
                <StackPanel Orientation="Horizontal">
                    <Border  x:Name="br" VerticalAlignment="Center">
                        <Image x:Name="icon" Width="16" Height="16" Stretch="UniformToFill" Source=""/>
                    </Border>
                    <TextBlock x:Name="txt">
                        <TextBlock.Text>
                            <MultiBinding Converter="{StaticResource ElementNameToLangStringConverter}">
                                <Binding Path="Name"/>
                                <Binding Path="Language.Lang"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>           


        </ResourceDictionary>
    </UserControl.Resources>


    <Border BorderBrush="Gray" BorderThickness="1" CornerRadius="2" Margin="2">
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <TreeView x:Name="SignalTree"  BorderThickness="1,0,0,0" 
                  ItemsSource="{Binding UserVMList}" ItemTemplate="{StaticResource RootTemplate}" 
                  cal:Message.Attach="[Event SelectedItemChanged] = [Action SelectedItemChanged($view,$eventArgs)]">
                <e:Interaction.Behaviors>
                    <visual:BindableSelectedItemBehavior SelectedItem="{Binding SelectedUser, Mode=TwoWay}" />
                </e:Interaction.Behaviors>
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">

                        <Setter Property="IsExpanded" Value="True" />
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                        <Setter Property="AllowDrop" Value="True"/>
                        <EventSetter Event="PreviewMouseDown" Handler="UserItem_PreviewMouseDown"/>
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                <ContextMenu ItemsSource="{Binding ContextMenu}">
                                    <ContextMenu.ItemContainerStyle>
                                        <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MenuItemBC}">
                                            <Setter Property="Header" Value="{Binding Name}"/>
                                            <Setter Property="cal:Message.Attach" Value="[Event Click] = [Action MenuItem_Click($view,$eventArgs)]"/>
                                        </Style>
                                    </ContextMenu.ItemContainerStyle>
                                </ContextMenu>
                            </Setter.Value>
                        </Setter>

                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="FontWeight" Value="Bold"/>
                                <Setter Property="Foreground" Value="White"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>

            </TreeView>
        </ScrollViewer>
    </Border>
</UserControl>

2.cs中的部分函数实现

public void SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
       {
           UserItemViewProperty sg = e.NewValue as UserItemViewProperty;
           if (sg != null)
           {
               GetNodeDeviceList(sg);
           }
       }

        protected void GetNodeDeviceList(UserItemViewProperty nowsg)
        {
            if (nowsg.Authority.AuthorityNum < 0)
            {
                LookVMList.Clear();
                var list = GetChildren(nowsg);
                int idex = 1;
                foreach (var child in list)
                {
                    child.Idex = idex++;
                    LookVMList.Add(child);
                }
            }
            else
            {
                LookVMList.Clear();
                nowsg.Idex = 1;
                LookVMList.Add(nowsg);
            }
        }

        private UserItemViewProperty[] GetChildren(UserItemViewProperty user)
        {
            List<UserItemViewProperty> list = new List<UserItemViewProperty>();
            foreach (var child in user.Children)
            {
                if (child.Authority.AuthorityNum < 0)
                {
                    list.AddRange(GetChildren(child));
                }
                else
                    list.Add(child);
            }

            return list.ToArray<UserItemViewProperty>();
        }
展开阅读全文

没有更多推荐了,返回首页