所以我有这个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属性:
`DisplayMemberBinding="{Binding IsOK }"我希望为真image图像显示特定的and specific图像,用于false。
有什么建议吗?
发布于 2017-06-23 19:33:50
使用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>我现在不能测试它,我发现这个解决方案可能会出错,因为我记不起答案了:
但基本上,答案是DataTrigger。或者一个转换器,它将接受IsOK并根据值返回一个图像。
发布于 2017-06-23 19:38:07
使用这样的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>https://stackoverflow.com/questions/44728648
复制相似问题