我有两个布尔,我想根据它们的价值显示图像,如下所示:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<Image Visibility="{Binding (Boolean1 && Boolean2),Converter={StaticResource BooleanToVisibilityConverter}}" />
注意Boolean1和Boolean2表达式。
答案 0 :(得分:12)
XAML中没有定义&&
运算符,但您可以绑定到多个属性并使用IMultiValueConverter
:
<Image>
<Image.Visibility>
<MultiBinding Converter="{StaticResource YourMultiConverter}">
<Binding Path="Boolean1" />
<Binding Path="Boolean2" />
</MultiBinding>
</Image.Visibility>
</Image>
public class YourMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool a = (bool)values[0];
bool b = (bool)values[1];
return a && b ? Visibility.Visible : Visibility.Collapsed;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
或者您可以在条件中使用Image
样式:
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Boolean1}" Value="True" />
<Condition Binding="{Binding Boolean2}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Visible" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
答案 1 :(得分:1)
mm8已经提供了正确的答案,但是这会有一点点改进
public class LogicalAndConverter : IMultiValueConverter
{
public IValueConverter FinalConverter{get;set;}
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool rtn = values.All(v=>(bool)v);
if(FinalConverter==null)
return rtn;
else
FinalConverter.Convert(rtn,targetType,parameter,culture);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后允许您使用不同的转换器而无需重写多路转换器
<local:LogicalAndConverter x:Key="LogicalAndConverter ">
<local:LogicalAndConverter.FinalConverter>
<BooleanToVisibilityConverter />
</local:LogicalAndConverter.FinalConverter>
</local:LogicalAndConverter>
答案 2 :(得分:0)
您可以使用多值转换器来解决此问题
XAML:
<TextBlock Name="textBlockOutput" Grid.Row="4" Grid.Column="2">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MultiValueConverter}">
<Binding Path="TextOne" />
<Binding Path="TextTwo" />
<Binding Path="TextThree" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
public class MultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string one = values[0] as string;
string two = values[1] as string;
string three = values[2] as string;
if(!string.IsNullOrEmpty(one) && !string.IsNullOrEmpty(two) && !string.IsNullOrEmpty(three))
{
return one + two + three;
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
答案 3 :(得分:0)
如果您不介意向项目中添加另一个包,则DevExpress的MVVM框架(可免费获得)具有名为DXBinding的绑定标记扩展,该扩展允许用户编写绑定函数/表达式。
<Image Visibility="{DXBinding 'Boolean1 and Boolean2', Converter={StaticResource BooleanToVisibilityConverter}}"/>