GridViewColumn图像基于条件

时间:2017-06-23 19:25:26

标签: wpf listview

所以我有var timeOut; document.getElementById("foo").addEventListener("input", function() { clearTimeout(timeOut); timeOut = setTimeout(function() { document.getElementById("bar").style.display = "block"; }, 1000); });

override init()

我的班级

override init() {
    self.image = NSImage(named: "buttonImage.png")
}

所以我想绑定我的GridViewColumn属性:

<GridViewColumn Width="60" Header="Checksum">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Image Width="18" Height="18" Source="pack://application:,,,/Resources/image.ico"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

我希望将真实public class MyData { public bool IsOK {ger; set;} } 图片`的特定bool显示为false。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

使用DataTrigger

<GridViewColumn Width="60" Header="Checksum">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <DataTemplate.Trigger>
                <DataTrigger Binding="{Binding IsOK}" Value="True">
                    <Setter TargetName="myImage" Property="Source" Value="pack://application:,,,/Resources/true.ico"/> 
                </DataTrigger>
                <DataTrigger Binding="{Binding IsOK}" Value="False">
                    <Setter TargetName="myImage" Property="Source" Value="pack://application:,,,/Resources/false.ico"/> 
                </DataTrigger>
            </DataTemplate.Trigger>
            <Image Width="18" Height="18" x:Name="myImage"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

我现在无法对它进行测试,我发现这个解决方案可能出现问题,因为我不记得这些答案:

  • 你能用触发器中的字符串设置源值吗?

  • 您可以在datatemplate中按名称引用元素吗?

但基本上,答案是DataTrigger。或转换器将采用IsOK并根据值返回图像。

答案 1 :(得分:1)

像这样使用IValueConverter

public class BoolToPathConverter : IValueConverter
{
    public string TruePath
    {
        get;
        set;
    } = "DefaultTrueImagePath";

    public string FalsePath
    {
        get;
        set;
    } = "DefaultFalseImagePath";

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            bool val = (bool)value;

            return val ? TruePath : FalsePath;
        }
        else
        {
            return value;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

最后你会有这样的事情:

  <Window.Resources>
        <!-- local: is the xmlns namespace of the converter  -->
        <local:BoolToPathConverter x:Key="BoolToPathConverter" TruePath="MyTruePath" FalsePath="MyFalsePath" />
    </Window.Resources>

    <Grid>
        <Image Source="{Binding Path=IsOk, Converter={StaticResource BoolToPathConverter}}" />
    </Grid>