WPF 可见性绑定到具有多个变量的布尔表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42342950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Visiblity Binding to Boolean Expression with multiple Variables
提问by Stacker
I have two Booleans I want to show Image based on their value, something like this :
我有两个布尔值,我想根据它们的值显示图像,如下所示:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<Image Visibility="{Binding (Boolean1 && Boolean2),Converter={StaticResource BooleanToVisibilityConverter}}" />
Notice Boolean1 and Boolean2 expression.
注意 Boolean1 和 Boolean2 表达式。
回答by mm8
There is no &&operator defined in XAML but you could bind to several properties and use an IMultiValueConverter:
&&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();
}
}
Or you could use an Imagestyle with conditions:
或者您可以使用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>
回答by MikeT
mm8 has provided the correct answer however a slight improvement would be this
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();
}
}
this then allows you do use different converters with out rewriting the multiconverter
这样您就可以使用不同的转换器而无需重写多转换器
<local:LogicalAndConverter x:Key="LogicalAndConverter ">
<local:LogicalAndConverter.FinalConverter>
<BooleanToVisibilityConverter />
</local:LogicalAndConverter.FinalConverter>
</local:LogicalAndConverter>
回答by voon
If you don't mind adding another package to your project, DevExpress' MVVM framework (which is freely available), has a binding markup extension called DXBindingthat allows one to write binding functions / expressions.
如果您不介意在您的项目中添加另一个包,DevExpress 的 MVVM 框架(可免费获得)有一个称为DXBinding的绑定标记扩展,它允许编写绑定函数/表达式。
<Image Visibility="{DXBinding 'Boolean1 and Boolean2', Converter={StaticResource BooleanToVisibilityConverter}}"/>
回答by Ragavan
You can use multivalue converter to resolve this issue
您可以使用多值转换器来解决此问题
XAML :
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();
}
}

