WPF:Treeview Item Root使用不同的模板然后使用子节点

时间:2021-01-11 12:40:23

I currently am trying to bind my business object to a treeview as the root. And its collection property as the child. [I want to achieve this via BINDING]

我目前正在尝试将我的业务对象绑定到树视图作为根。而它的收藏属性就像孩子一样。 [我希望通过BINDING实现这一目标]

Something like this.

像这样的东西。

public object MyBusinessObject
{
   private int _number;
   private bool _isSelected;
   private ObservableCollection<AnotherObject> _other = new ObservableCollection<AnotherObject>();


   public int Number { get {return _number;} set {_number = value;}}
   public bool IsSelected{ get {return _isSelected;} set {_isSelected= value;}}
   public ObservableCollection<AnotherObject>  Children { get {return _other;}}

}

I want my treeview to be represented like this:

我希望我的treeview像这样表示:

  • "CheckBox binded to IsSelected" "Text binded to Number"
    • List of child binded to my "Children"
    • 绑定到我的“孩子”的孩子一览
    • List of child binded to my "Children"
    • 绑定到我的“孩子”的孩子一览
    • List of child binded to my "Children"
    • 绑定到我的“孩子”的孩子一览
  • “CheckBox绑定到IsSelected”“文本绑定到数字”绑定到我的“孩子”的孩子的列表绑定到我的“孩子”的孩子的列表绑定到我的“孩子”的孩子的列表
  • "CheckBox binded to IsSelected" "Text binded to Number"
    • List of child binded to my "Children"
    • 绑定到我的“孩子”的孩子一览
    • List of child binded to my "Children"
    • 绑定到我的“孩子”的孩子一览
    • List of child binded to my "Children"
    • 绑定到我的“孩子”的孩子一览
  • “CheckBox绑定到IsSelected”“文本绑定到数字”绑定到我的“孩子”的孩子的列表绑定到我的“孩子”的孩子的列表绑定到我的“孩子”的孩子的列表

I have no idea how to do this in xaml:

我不知道如何在xaml中执行此操作:

  <TreeView x:Name="_tv" ItemsSource="{Binding Path=MyBusinessObject}" >

            <TreeView.Resources>
                <HierarchicalDataTemplate> 
                    <CheckBox Content="{Binding Path=Number} IsChecked="{Binding Path=IsSelected}" />
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                    <TextBlock Text="{Binding Path=Name}" />
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>

I know the above is not right, but i was wondering if there is a way to do this properly.

我知道以上是不对的,但我想知道是否有办法正确地做到这一点。

Thanks and Regards,

感谢致敬,

1 个解决方案

#1


5  

Sure, you can use the HierarchicalDataTemplate.ItemTemplate property to define the data template to be used for the collection of AnotherObject instances.

当然,您可以使用HierarchicalDataTemplate.ItemTemplate属性来定义要用于AnotherObject实例集合的数据模板。

<TreeView ItemsSource="{Binding SomeCollectionOfObjects}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">

            <!-- This is used for your AnotherObject instances -->
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>

            <!-- This is used for your MyBusinessObject instances -->
            <CheckBox Content="{Binding Number}" IsChecked="{Binding IsSelected}" />

        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

#1


5  

Sure, you can use the HierarchicalDataTemplate.ItemTemplate property to define the data template to be used for the collection of AnotherObject instances.

当然,您可以使用HierarchicalDataTemplate.ItemTemplate属性来定义要用于AnotherObject实例集合的数据模板。

<TreeView ItemsSource="{Binding SomeCollectionOfObjects}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">

            <!-- This is used for your AnotherObject instances -->
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>

            <!-- This is used for your MyBusinessObject instances -->
            <CheckBox Content="{Binding Number}" IsChecked="{Binding IsSelected}" />

        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>