【发布时间】:2012-07-13 07:17:16
【问题描述】:
我想要类似于 here 所要求的内容 - 但是,我需要模板依赖于属性的值,即枚举。
这个类看起来和这个很相似:
class ResultBlock
{
public string Name { get; set; }
public BlockType Type { get; set; }
public IList<ResultBlock> ChildBlocks { get; private set; }
}
BlockType 具有三个不同的值,BLOCK, FILE, FOLDER - 现在,我想创建一个数据模板以不同的方式呈现,具体取决于 ResultBlock.Type 在当前对象中的值。
我尝试使用DataType= 执行此操作,但显然没有成功。我确信有一些方法可以仅在 XAML 中很容易地做到这一点。
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type docom:ResultBlock}" ItemsSource="{Binding ChildBlocks}">
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<DataTemplate DataType="{x:Type docom:BlockType.BLOCK}">
<TextBlock Text="BLOCK:{Binding Name}" />
</DataTemplate>
</StackPanel.Resources>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
【问题讨论】:
-
如果只有
BLOCK:前缀发生变化,您可以简单地将TextBlock绑定到BlockType属性。
标签: c# wpf xaml data-binding