扩大以下问题:
Defining a Property in a IValueConverter class
我的问题是:
在xaml文件中,TrueValue设置为单个值:
<CheckBox IsChecked="{Binding item, Converter={converter:ValueConverterWithProperties TrueValue=5}}"></CheckBox>是否可以将ValueConverter中的属性绑定到某种列表中?绑定表达式会是什么样的呢?
发布于 2018-01-31 10:53:22
可以在转换器类中声明依赖项属性,将转换器声明为静态资源,并将该属性绑定到视图模型属性。
这将起作用:
<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;
}
}发布于 2018-01-31 10:12:02
绑定到IValueConverter的参数或属性不起作用,但您可以使用IMultiValueConverter并将附加值绑定到所需的属性:
<MultiBinding Converter="{StaticResource MultiValueConverter}">
<Binding Path="YourValue" />
<Binding Path="YourParameter" />
</MultiBinding>然后使用values[0]作为实际值,values[1]作为参数。
https://stackoverflow.com/questions/48539313
复制相似问题