简体   繁体   English

更改依赖于items对象的TreeViewItem模板

[英]Changing a TreeViewItem template dpending upon the items object

I have a TreeView bound to an observable collection of one type of object but I want to display a different DataTemplate based upon a value within that object rather than on the main object type because they are the same type. 我有一个TreeView绑定到一种对象类型的可观察集合,但是我想基于该对象内的值而不是主要对象类型来显示不同的DataTemplate,因为它们是同一类型。 Further more and to clarify, I actually want to base it upon the type of object it contains. 更进一步并澄清一下,我实际上想基于它包含的对象类型。

Ok, so, rather than doing this:
<HierarchicalDataTemplate DataType="{x:Type listItem:TreeNodeViewModel}" ItemsSource="{Binding     Children}"

I actually want to do this:

<HierarchicalDataTemplate DataType.EmbeddedItem="{x:Type listItem:MyEmbededObjectType}"ItemsSource="{Binding Children}"

I have searched all day and now asking the question.

Hello and thanks for your answer but I am still a little unsure what to do here. 您好,感谢您的回答,但我仍然不确定在这里做什么。 Also, I have just found out how to format the question so that it is all displayed and will make it easier to know what I am trying to do. 另外,我刚刚发现了如何格式化问题,以便将其全部显示出来,并使您更容易知道我要做什么。 I am new to Wpf but a long time developer and because of that I am very quickly getting to grips with things but there are the odd thing that stumps me. 我是Wpf的新手,但是长期从事开发人员,因此我很快就可以掌握问题,但是有些奇怪的事情使我感到困惑。 I have a TreeView that displays a different template if the objects in the ObservableCollection are different objects but in this case the problem that in this case the objects are all of the same type in the ObservableCollection but each of these objects have an embedded object of a different type and that is what I am trying to get XAML to see as the object type in which to retrieve the template. 如果ObservableCollection中的对象是不同的对象,我有一个TreeView显示不同的模板,但是在这种情况下,对象在ObservableCollection中都是相同类型的,但是每个对象都有一个嵌入式对象不同的类型,这就是我试图将XAML视为检索模板的对象类型的原因。 So, the objects I am providing in the ObservableCollection are TreeNodeViewModel. 因此,我在ObservableCollection中提供的对象是TreeNodeViewModel。 This class has an embedded object of type object called Item. 此类具有一个称为Item的object类型的嵌入式对象。 It is the bound item that I use to know what the TreeNode is related to. 这是我用来了解TreeNode与之相关的绑定项。 This property is called Item and it is this property I want to use to lift up the template. 此属性称为Item,这是我要用来提升模板的属性。 Thanks in advance. 提前致谢。

Put both objects in your DataTemplate, and use an IValueConverter to set the Visibility. 将两个对象都放在DataTemplate中,并使用IValueConverter设置可见性。

<DataTemplate>
  <StackPanel>
    <Button x:Name="Object1ShowThis" Visibility={StaticResource cMakeValueConverter/>
    <Button x:Name="Object2ShowThis" Visibility={StaticResource cMakeValueConverter2/>
   </StackPanel>
<DataTemplate>

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

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