WPF TreeView 树型控件绑定 Binding

Posted 胡文杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF TreeView 树型控件绑定 Binding相关的知识,希望对你有一定的参考价值。

XAML:

<Window.DataContext>
    <local:VMTempTest/>
</Window.DataContext>
<Grid>
    <StackPanel Margin="10">
        <TextBlock Text="树" FontWeight="Bold"  Margin="0,5,0,5" ></TextBlock>
        <DockPanel x:Name="TreeButton" >
            <StackPanel DockPanel.Dock="Left" >
                <TreeView ItemsSource="{Binding TreeInfo}" x:Name="tree" BorderThickness="0" FontSize="16">
                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                            <TextBlock Text="{Binding NodeName}"/>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>
            </StackPanel>

            <StackPanel DockPanel.Dock="Right" Margin="50 0 0 0" VerticalAlignment="Center" Orientation="Horizontal" DataContext="{Binding SelectedItem,ElementName=tree}">
                <TextBlock Text="结果:"/>
                <TextBlock Text="{Binding NodeID,StringFormat=\'NodeID:\\{0\\}\'}"  Margin="0,0,20,0"  />
                <TextBlock Text="{Binding NodeName,StringFormat=\'NodeName:\\{0\\}\'}"/>
            </StackPanel>
        </DockPanel>
    </StackPanel>
</Grid>

Models:

public class TreeNodeModel : ObservableObject
{
    public string NodeID { get; set; }
    public string NodeName { get; set; }
    public List<TreeNodeModel> Children { get; set; }
}

ViewModel:

public class VMTempTest : ViewModelBase
{
    public VMTempTest()
    {
        TreeInfo = new List<TreeNodeModel>()
        {
            new TreeNodeModel(){
              NodeID = "1", NodeName = "苹果",Children=new List<TreeNodeModel>(){
                new TreeNodeModel(){ NodeID="1.1", NodeName ="苹果A" },
                new TreeNodeModel(){ NodeID="1.2", NodeName ="苹果B" },
                new TreeNodeModel(){ NodeID="1.3", NodeName ="苹果C",Children = new List<TreeNodeModel>(){
                   new TreeNodeModel(){ NodeID="1.3.1", NodeName ="苹果C1" },
                   new TreeNodeModel(){ NodeID="1.3.2", NodeName ="苹果C2" },
                } },

              }
            },
            new TreeNodeModel(){
              NodeID = "2", NodeName = "香蕉",Children=new List<TreeNodeModel>(){
                new TreeNodeModel(){ NodeID="2.1", NodeName ="香蕉A" },
                new TreeNodeModel(){ NodeID="2.2", NodeName ="香蕉B" },
                new TreeNodeModel(){ NodeID="2.3", NodeName ="香蕉C" }
              }
            }
        };
    }

    private List<TreeNodeModel> treeInfo;
    /// <summary>
    /// 树控件数据信息
    /// </summary>
    public List<TreeNodeModel> TreeInfo
    {
        get { return treeInfo; }
        set { treeInfo = value; RaisePropertyChanged(() => TreeInfo); }
    }
}



以上是关于WPF TreeView 树型控件绑定 Binding的主要内容,如果未能解决你的问题,请参考以下文章

WPF中TreeView控件数据绑定和后台动态添加数据

WPF 之 TreeView节点重命名

delphi中treeview的用法

treeview控件

wpf中怎么为treeview动态添加子菜单

WPF中TreeView的使用