条件ItemsControl.ItemTemplate绑定 [英] Conditional ItemsControl.ItemTemplate binding

查看:60
本文介绍了条件ItemsControl.ItemTemplate绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,并且它是多余的-我试图完全在没有代码隐藏的情况下完成它.

I'm working on a project and as redundant as it is - I'm trying to do it entirely without code-behind.

我有一个名为MessagePanel的用户控件,该控件用于包装通过TCP连接接收的消息.

I have a User Control called MessagePanel that's meant to wrap messages received through the TCP connection.

消息可以是纯文本或仅图像,并且我的控件旨在使用不同的数据模板来处理这两种消息.

Messages can either be text-only or image-only and my control is meant to handle both using different data templates.

文本模板:

<ItemsControl ItemsSource="{Binding Messages}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Text}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

图片模板:

<ItemsControl ItemsSource="{Binding Messages}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Source="{Binding Image}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我在根据IsImage布尔属性确定如何触发它们中的任何一个时遇到问题.

I'm having an issue figuring out how to trigger for either of them to be used based on a IsImage boolean property.

我将不胜感激.

推荐答案

有几种方法可以实现此目的,通常您会使用分配给ItemsControl的Item​Template​Selector属性的DataTemplateSelector.

There are several ways to achieve this, and you would typically use a DataTemplateSelector that is assigned to the ItemsControl's Item​Template​Selector property.

但是,您可以在ItemsControl的ItemContainerStyle中使用DataTrigger编写仅XAML解决方案:

You may however write a XAML-only solution with a DataTrigger in the ItemContainerStyle of the ItemsControl:

<ItemsControl ItemsSource="{Binding Messages}">
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}"/>
                            <TextBlock Text="{Binding Text}"/>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsImage}" Value="True">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Image Source="{Binding Image}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

请注意,您可能不需要具有IsImage属性. DataTrigger还可以检查nullImage属性:

Note that you might probably not need to have an IsImage property. The DataTrigger could as well check the Image property for null:

<DataTrigger Binding="{Binding Image}" Value="{x:Null}">

这篇关于条件ItemsControl.ItemTemplate绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆