具有依赖项属性的转换器 [英] Converter with Dependency Properties

查看:59
本文介绍了具有依赖项属性的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现自定义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!

这篇关于具有依赖项属性的转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆