我正在尝试解决一个问题,即无法为
为了解决这个问题,我创建了一个自定义转换器,并在该转换器上公开了一个依赖属性:
到目前为止,我看到的例子只绑定静态资源。
编辑:
我得到了一些反馈,我的 XAML 只有一个转换器实例。
我可以通过将资源放在我的控件中来解决这个问题。
所以这个问题不会因为需要共享转换器的实例而被阻塞 :)
ConverterParameter
指定动态值。请参阅我的其他问题,了解为什么需要将动态值绑定到ConverterParameter
- 我不喜欢当前发布的解决方案,因为它们都需要对我的视图模型进行我认为不必要的更改。为了解决这个问题,我创建了一个自定义转换器,并在该转换器上公开了一个依赖属性:
public class InstanceToBooleanConverter : DependencyObject, IValueConverter
{
public object Value
{
get { return (object)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(InstanceToBooleanConverter), null);
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value != null && value.Equals(Value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? Value : Binding.DoNothing;
}
}
有没有一种方法可以在我的XAML中使用绑定(或样式设置器,或其他疯狂的方法)来设置这个值?
<ItemsControl ItemsSource="{Binding Properties}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:SomeClass}">
<DataTemplate.Resources>
<!-- I'd like to set Value to the item from ItemsSource -->
<local:InstanceToBooleanConverter x:Key="converter" Value="{Binding Path=???}" />
</DataTemplate.Resources>
<!- ... ->
到目前为止,我看到的例子只绑定静态资源。
编辑:
我得到了一些反馈,我的 XAML 只有一个转换器实例。
我可以通过将资源放在我的控件中来解决这个问题。
<ItemsControl ItemsSource="{Binding Properties}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:SomeClass}">
<RadioButton Content="{Binding Name}" GroupName="Properties">
<RadioButton.Resources>
<!-- I'd like to set Value to the item from ItemsSource -->
<local:InstanceToBooleanConverter x:Key="converter"
Value="{Binding Path=???}" />
</RadioButton.Resources>
<RadioButton.IsChecked>
<Binding Path="DataContext.SelectedItem"
RelativeSource="{RelativeSource AncestorType={x:Type Window}}"
Converter="{StaticResource converter}" />
</RadioButton.IsChecked>
</RadioButton>
<!- ... ->
所以这个问题不会因为需要共享转换器的实例而被阻塞 :)