问题描述
我有我想要显示/隐藏,这取决于一个布尔值的控制。
I have a control that I want to show/hide, depending on the value of a boolean.
我有一个NegatedBooleanConverter(切换时真亦假,反之亦然),我需要先运行该转换器。
我有一个BooleanToVisibilityConverter,我需要的NegatedBoolConverter后运行该转换器。
I have a NegatedBooleanConverter (switches true to false and vice versa) and I need to run this converter first.I have a BooleanToVisibilityConverter and I need to run this converter after the NegatedBoolConverter.
我怎样才能解决这个问题呢?我想这样做在XAML。
How can I fix this problem? I want to do this in XAML.
这似乎并没有工作。它首先将在单独的转换器的值,然后做与转换价值的东西。
That doesn't seem to work. It first converts the value with the seperate converters and then does something with the converted values.
我需要的是:
- 与第一个转换器,转换价值(这给convertedValue)。
- 转换convertedValue与第二个转换器,它的这个结果,我所需要的。
推荐答案
这是我做过什么:
public class CombiningConverter : IValueConverter
{
public IValueConverter Converter1 { get; set; }
public IValueConverter Converter2 { get; set; }
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
object convertedValue = Converter1.Convert(value, targetType, parameter, culture);
return Converter2.Convert(convertedValue, targetType, parameter, culture);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
和我叫它是这样的:
<converters:CombiningConverter x:Key="negatedBoolToVisibilityConverter" Converter1="{StaticResource NegatedBooleanConverter}" Converter2="{StaticResource BoolToVisibilityConverter}" />
一个MultiValueConverter或许还可以,我认为。也许我会尝试更高版本。
A MultiValueConverter might also be possible I think. Maybe I'll try that later.
这篇关于WPF:如何使用2转换器1的结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!