问题
I'm trying to get around the fact that I can't specify a dynamic value for ConverterParameter
. See my other question for why I need to bind a dynamic value to ConverterParameter - I don't like the solutions currently posted because they all require what I feel should be unnecessary changes to my View Model.
To attempt to solve this, I have created a custom converter, and exposed a dependency property on that converter:
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;
}
}
Is there a way to set this value using a binding (or style setter, or other crazy method) in my 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>
<!- ... ->
The examples I've seen so far only bind to static resources.
Edit:
I got some feedback that there is only one converter instance with the XAML I posted.
I can work around this by placing the resource in my control:
<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>
<!- ... ->
So this problem isn't blocked by having to share an instance of the converter :)
回答1:
Unfortunately this isn't going to work - I've been down this road before and it turns out all the Items in the ItemsControl share the same Converter. I think this is due to the way the XAML parser works.
回答2:
Firstly you can specify the converter at a higher level resource dictionary and set x:Shared to false
, secondly if you want to "set the Value to the item from ItemsSource" as you annotated you can just specify an empty binding (Value="{Binding}"
).
来源:https://stackoverflow.com/questions/5903402/how-can-i-set-a-dependency-property-on-a-static-resource