简体   繁体   English

WPF:如何将Listview单元格内容与datatrigger(或等效的)交换

[英]WPF: How to swap Listview cell content with a datatrigger (or equivalent)

I have a WPF App that implements a ListView. 我有一个实现ListView的WPF应用程序。 I would like to show an image (a small icon) in one of the columns depending on the type of the data that row represents. 我想在其中一列中显示一个图像(一个小图标),具体取决于该行所代表的数据类型。 Sort of like the display you see in Windows Explorer. 有点像您在Windows资源管理器中看到的显示。

I am using DataTriggers elsewhere in my XAML, it seems like a similar method could be used to swap out entire cell contents, but I can't find an example of anyone doing that. 我在我的XAML中的其他地方使用DataTriggers,似乎可以使用类似的方法来交换整个单元格内容,但我找不到任何人这样做的示例。

Any thoughts? 有什么想法吗?

There are three common techniques for this. 有三种常见的技术。

1) DataTrigger: 1)DataTrigger:

<DataTemplate x:Key="ImageColumn">
    <Grid>
        <Image x:Name="img" Source="MyImage.png"/>
        <Rectangle x:Name="rect" Fill="Red" Visibility="Hidden"/>
    </Grid>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding TriggerProperty}" Value="2">
            <Setter TargetName="rect" Property="Visibility" Value="Visible"/>
            <Setter TargetName="img" Property="Visibility" Value="Hidden"/>
        </DataTrigger>
        <!--etc...-->
    </DataTemplate.Triggers>
</DataTemplate>

2) ValueConverters: 2)ValueConverters:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string strVal = value as string;
        switch (strVal)
        {
            case "2":
                Rectangle rect = new Rectangle();
                rect.Fill = Brushes.Red;
                return rect;

            default:
                Image img = new Image();
                ImageSourceConverter s = new ImageSourceConverter();
                img.Source = (ImageSource)s.ConvertFromString("MyImage.png");
                return img;
        }
    }
}

3) MVVM (Model-View-ViewModel): 3)MVVM(Model-View-ViewModel):

Create a ViewModel class that wraps your data model. 创建一个包装数据模型的ViewModel类。 This ViewModel would evaulate the properties in the data model and combine them with logic into a new property. 此ViewModel将评估数据模型中的属性,并将它们与逻辑组合到一个新属性中。

public UIElement Icon
{
    get
    {
        if (TriggerProperty == "2")
        {
            Rectange rect = new Rectangle();
            rect.Fill = Brushes.Red;
            return rect;
        }

        else
        {
            Image img = new Image();
            ImageSourceConverter s = new ImageSourceConverter();
            img.Source = (ImageSource)s.ConvertFromString("MyImage.png");
            return img;
        }
    }
}

And the XAML: 和XAML:

<DataTemplate x:Key="ImageColumn">
    <ContentControl Content="{Binding Icon}"/>
</DataTemplate>

In the past, I've used ValueConverters to provide the Image I want to display, however I am intrigued to the possibility of using DataTriggers for this purpose. 在过去,我使用ValueConverters来提供我想要显示的图像,但是我很想知道为此目的使用DataTriggers的可能性。

Beatriz Stollnitz posts a solution to a similar issue here . Beatriz Stollnitz 在这里发布了类似问题的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM