具有属性的ValueConverter [英] ValueConverter with properties
本文介绍了具有属性的ValueConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
延伸以下问题:
我的问题是:
在xaml文件中,TrueValue设置为单个值:
In the xaml file, TrueValue is set to a single value:
<CheckBox IsChecked="{Binding item, Converter={converter:ValueConverterWithProperties TrueValue=5}}"></CheckBox>
是否可以将ValueConverter中的属性绑定到某种列表? 绑定表达式看起来如何?
Is it possible to bind a property in a ValueConverter to some kind of List? How would the binding expression look like?
推荐答案
您可以在转换器类中声明依赖项属性,将转换器声明为静态资源,然后将该属性绑定到视图模型属性.
You can declare a dependency property in the converter class, declare your converter as static resource and bind the property to a view model property.
这将起作用:
<Window x:Class="..."
x:Name="_this"
...>
<Window.Resources>
<local:DepPropConverter x:Key="Convert"
MyList="{Binding DataContext.YourListInViewmodel, Source={x:Reference _this}}"/>
</Window.Resources>
<CheckBox IsChecked="{Binding item, Converter={StaticResource Converter}}"></CheckBox>
转换器:
public class DepPropConverter : DependencyObject, IValueConverter
{
public static readonly DependencyProperty MyListProperty =
DependencyProperty.Register(
nameof(MyList), typeof(IList), typeof(DepPropConverter));
public IList MyList
{
get { return (IList)GetValue(MyListProperty); }
set { SetValue(MyListProperty, value); }
}
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
//your logic here
return value;
}
public object ConvertBack(
object value, Type targetTypes, object parameter, CultureInfo culture)
{
//your logic here
return value;
}
}
这篇关于具有属性的ValueConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文