如何在静态资源上设置依赖属性? [英] How can I set a dependency property on a static resource?
问题描述
我想解决一个事实,即我不能为 ConverterParameter
指定动态值。 查看我的其他问题,为什么我需要将动态值绑定到 ConverterParameter
- 我不喜欢目前发布的解决方案,因为它们都需要我觉得应该是我的View Model不必要的更改。
要尝试解决这个问题,我已经创建了一个自定义转换器,并在该转换器上暴露了依赖属性:
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)?价值:Binding.DoNothing;
}
}
有没有办法使用绑定设置此值(或风格设定者或其他疯狂方法)在我的XAML?
< ItemsControl ItemsSource ={Binding Properties}> ;
< ItemsControl.ItemTemplate>
< DataTemplate DataType ={x:Type local:SomeClass}>
< DataTemplate.Resources>
<! - 我想从ItemsSource - >中的项目设置Value
< local:InstanceToBooleanConverter x:Key =converterValue ={Binding Path = ???}/>
< /DataTemplate.Resources>
<! - ... - >
到目前为止,我看到的示例只绑定到静态资源。
编辑:
我收到一些反馈,表示只有一个转换器实例与我发布的XAML。 / p>
我可以通过将资源置于我的控制范围内来解决:
< ItemsControl ItemsSource ={Binding Properties}>
< ItemsControl.ItemTemplate>
< DataTemplate DataType ={x:Type local:SomeClass}>
< RadioButton Content ={Binding Name}GroupName =Properties>
< RadioButton.Resources>
<! - 我想从ItemsSource - >中的项目设置Value
< 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>
<! - ... - >
所以这个问题不被阻止,因为不必共享转换器的一个实例:)
首先,您可以在更高级别的资源字典中指定转换器,并设置 x:共享
到 false
,其次如果你想注释中的将项目从ItemsSource 设置为,你可以指定一个空的绑定( Value ={Binding}
)。
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 :)
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}"
).
这篇关于如何在静态资源上设置依赖属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!