我想要的東西類似於要求的內容here - 但是,我需要模板依賴於屬性的值,這是一個枚舉值。不同的DataTemplate取決於屬性的枚舉值
類看起來simlar這樣:
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」屬性。 – 2012-07-24 14:19:22