具有依赖项属性的转换器

编程入门 行业动态 更新时间:2024-10-25 23:28:27
本文介绍了具有依赖项属性的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在实现自定义DependencyObject时遇到问题:

I have problems implementing a custom DependencyObject:

我需要一个在绑定属性中设置或取消枚举标记的转换器。因此,我创建了一个从FrameworkElement删除的IValueConverter,它具有两个DependencyProperty:Flag(由转换器设置/取消设置的标志)和Flags(要修改的值/属性)。父UserControl(名称= EnumerationEditor)提供了转换器绑定到的属性。

I need a converter which sets or unsets a enum flag in a bound property. Therefore I created a IValueConverter derieved from FrameworkElement with two DependencyProperties: Flag (the flag which is set/unset by the converter) and Flags (the value/property to modify). The parent UserControl (Name = EnumerationEditor) provides the property to which the converter is bound.

ListBox生成CheckBoxes和转换器实例,这些实例用于通过数据模板。每个CheckBox / converter实例都用于一个标志。我使用以下XAML代码:

A ListBox generates CheckBoxes and the converter instances which are used to modify the property via a DataTemplate. Each CheckBox/converter instance is used for one flag. I use the following XAML code:

<ListBox Name="Values" SelectionMode="Extended" BorderThickness="1" BorderBrush="Black" Padding="5"> <ListBox.ItemTemplate> <DataTemplate DataType="{x:Type system:Enum}"> <DataTemplate.Resources> <Label x:Key="myTestResource" x:Shared="False" Content="{Binding}" ToolTip="{Binding Path=Value, ElementName=EnumerationEditor}" Foreground="{Binding Path=Background, ElementName=EnumerationEditor}" Background="{Binding Path=Foreground, ElementName=EnumerationEditor}"/> <converters:EnumerationConverter x:Key="EnumerationConverter" x:Shared="False" Flag="{Binding}" Flags="{Binding Path=Value, ElementName=EnumerationEditor}"/> </DataTemplate.Resources> <StackPanel Orientation="Horizontal"> <CheckBox Content="{Binding}" IsChecked="{Binding Path=Value, ElementName=EnumerationEditor, Converter={StaticResource EnumerationConverter}}"/> <ContentPresenter Content="{StaticResource myTestResource}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

奇怪的是:Label可以正常工作-但转换器不能正常工作。我得到了错误:

System.Windows.Data错误:4:找不到引用为'ElementName = EnumerationEditor'的绑定源。 BindingExpression:Path = Value; DataItem = null;目标元素是 EnumerationConverter(名称=);目标属性是标志(类型为枚举)

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=EnumerationEditor'. BindingExpression:Path=Value; DataItem=null; target element is 'EnumerationConverter' (Name=''); target property is 'Flags' (type 'Enum')

我不明白为什么,绑定基本上是相同的。 。

I don't understand why, the binding is basically the same...

这是转换器的代码:

public class EnumerationConverter : FrameworkElement, IValueConverter { #region IValueConverter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return false; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return Parity.Space; } #endregion #region public Enum Flag { get; set; } public Enum Flag { get { return (Enum)this.GetValue(EnumerationConverter.FlagProperty); } set { this.SetValue(EnumerationConverter.FlagProperty, value); } } /// <summary> /// Dependency property for Flag. /// </summary> public static readonly DependencyProperty FlagProperty = DependencyProperty.Register("Flag", typeof(Enum), typeof(EnumerationConverter)); #endregion #region public Enum Flags { get; set; } public Enum Flags { get { return (Enum)this.GetValue(EnumerationConverter.FlagsProperty); } set { this.SetValue(EnumerationConverter.FlagsProperty, value); } } /// <summary> /// Dependency property for Flags. /// </summary> public static readonly DependencyProperty FlagsProperty = DependencyProperty.Register("Flags", typeof(Enum), typeof(EnumerationConverter)); #endregion }

推荐答案

结论

我决定使用两个UserControl来解决该问题。 FlagControl和EnumerationEditorControl。

I decided to solve the problem using two UserControls; FlagControl and EnumerationEditorControl.

FlagControl具有两个依赖项属性

The FlagControl has two dependency properties

  • 标记(系统.Enum):确定控件设置/清除了哪个标志
  • Value(System.Enum):绑定到设置/清除标志的属性/值。

EnumerationEditorControl具有一个依赖项属性:

The EnumerationEditorControl has one dependency property:

  • Value (System.Enum):设置标记的属性/值。

EnumerationEditorControl使用DataTemplate实例化FlagControls。 DataTemplate将FlagControl.Flag属性绑定到DataContext,并将FlagControl.Value属性绑定到EnumerationEditorControl.Value属性。

The EnumerationEditorControl uses a DataTemplate to instantiate FlagControls. The DataTemplate binds the FlagControl.Flag property to the DataContext and the FlagControl.Value property to the EnumerationEditorControl.Value property.

这样,我不需要转换器和逻辑显然是分开的。

This way I don't need a converter and logic is clearly separated.

感谢您的建议,评论和回复!

Thanks for the suggestions, comments and replies!

更多推荐

具有依赖项属性的转换器

本文发布于:2023-08-02 11:48:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换器   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!