本文介绍了WPF转换器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个转换器,可以在代码中设置如下属性:
I have a converter that in code I can set a property like:
tabAssumptions.SetBinding(UIElement.VisibilityProperty, new Binding("CurrentPhase.IsWholeScheme") { Converter = new BoolToVisibilityConverter { Inverse = true } });
在XAML中使用时,如何设置转换器的Inverse属性?
How can I set the Inverse property of the converter when used in XAML?
推荐答案
我的BoolToVisibilityConverter在下面.您可以将其用作资源:
My BoolToVisibilityConverter is below. You can either use it as a resource:
<converters:BoolToVisibilityConverter x:Key="FalseToHidden"
TrueValue="Visible" FalseValue="Hidden" />
或将其用作XAML中的MarkupExtension内联:
or use it as a MarkupExtension inline in your XAML:
Visibility="{Binding MyFlag,
Converter={vc:BoolToVisibilityConverter
FalseValue=Collapsed, TrueValue=Visible}}"
C#
public class BoolToVisibilityConverter : BoolToValueConverter<Visibility>
{
#region Constructors and Destructors
public BoolToVisibilityConverter()
{
this.TrueValue = Visibility.Visible;
this.FalseValue = Visibility.Collapsed;
}
#endregion
}
/// <summary>
/// Use as the base class for BoolToXXX style converters
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BoolToValueConverter<T> : MarkupExtension, IValueConverter
{
#region Constructors and Destructors
protected BoolToValueConverter()
{
this.TrueValue = default(T);
this.FalseValue = default(T);
}
#endregion
#region Public Properties
public T FalseValue { get; set; }
public T TrueValue { get; set; }
#endregion
#region Public Methods and Operators
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return System.Convert.ToBoolean(value) ? this.TrueValue : this.FalseValue;
}
// Override if necessary
public virtual object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return value.Equals(this.TrueValue);
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
#endregion
}
这篇关于WPF转换器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!