不同的DataTemplate取决于属性的枚举值

时间:2012-07-13 07:17:17

标签: c# wpf xaml data-binding

我想要类似于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>

3 个答案:

答案 0 :(得分:7)

您可以触发该属性,例如:

<HierarchicalDataTemplate DataType="{x:Type docom:ResultBlock}"
                          ItemsSource="{Binding ChildBlocks}">
    <ContentControl Content="{Binding}">
        <ContentControl.Style>
            <Style TargetType="ContentControl">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding BlockType}" Value="BLOCK"> 
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                     <!-- Data template for BLOCK blocks -->
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                    <!-- More triggers -->
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
</HierarchicalDataTemplate>

是的,这很冗长。 (您可以将不同类型的模板定义为键控资源,然后在Setters中引用它们

答案 1 :(得分:6)

<Window.Resources>
    <local:TaskListDataTemplateSelector x:Key="myDataTemplateSelector"/>
</Window.Resources>
<Grid>
    <ListBox Width="400" Margin="10"
     ItemsSource="{Binding Source={StaticResource myTodoList}}"
     ItemTemplateSelector="{StaticResource myDataTemplateSelector}"
     HorizontalContentAlignment="Stretch"/>
</Grid>
public class TaskListDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate
        SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is Task)
        {
            Task taskitem = item as Task;

            if (taskitem.Priority == 1)
                return
                    element.FindResource("importantTaskTemplate") as DataTemplate;
            else
                return
                    element.FindResource("myTaskTemplate") as DataTemplate;
        }

        return null;
    }
}

这是为ListBox实现的,但DataGrid / TreeView的想法可以相同。我希望这会有所帮助。

答案 2 :(得分:1)

一种方法是使用TemplateSelector并在选择器中执行您想要的操作,具体取决于您的BlockType。

或者你创建了像:

这样的wrapperviewmodels
 public class ResultBlockBlock{}
 public class ResultBlockFile{}
 public class ResultBlockFolder{}

然后你可以使用DataTemplate DataType方式

相关问题