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

F.P*_*F.P 2 c# data-binding wpf xaml

我想要类似于这里要求的 东西 - 但是,我需要模板依赖于属性的值,这是一个枚举.

这个类看起来很像这样:

class ResultBlock
{
    public string Name { get; set; }
    public BlockType Type { get; set; }
    public IList<ResultBlock> ChildBlocks { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

哪里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>
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 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>
Run Code Online (Sandbox Code Playgroud)

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


yo *_*han 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>
Run Code Online (Sandbox Code Playgroud)
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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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