简体   繁体   English

静态资源转换器上的属性未绑定

[英]Property on static resource converter not bound

I have a value converter with a property I would like to bind to, but the binding never happens, ie the dependency property in my value converter always is null . 我有一个具有要绑定到的属性的值转换器,但是绑定从未发生,即,我的值转换器中的依赖项属性始终为null
Background: I want to bind an enum to a combo box but have control over the text that is being displayed. 背景:我想将枚举绑定到组合框,但可以控制所显示的文本。

I implemented the value converter like this: 我实现了这样的值转换器:

public class EnumDisplayer : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty LocalizerProperty =
        DependencyProperty.Register(
            "Localizer", typeof(ILocalizer), typeof(EnumDisplayer),
            new PropertyMetadata(default(ILocalizer), OnLocalizerChanged));

    public ILocalizer Localizer
    {
        get { return (ILocalizer) GetValue(LocalizerProperty); }
        set { SetValue(LocalizerProperty, value); }
    }

    private static void OnLocalizerChanged(DependencyObject d, 
                                           DependencyPropertyChangedEventArgs e)
    {
        // ...
    }

    //...
}

And I bind it like this: 我这样绑定它:

<UserControl.Resources>
    <Common:EnumDisplayer x:Key="companyTypes"
                          Localizer="{Binding CompanyTypeEnumLocalizer}" />
    <!-- ... -->
</UserControl.Resources>

My class is an adapted version of the EnumDisplayer . 我的课程是EnumDisplayer的改编版本。

I fail to understand, why OnLocalizerChanged is never called. 我不明白,为什么从不调用OnLocalizerChanged Can anyone provide some insight? 谁能提供一些见识?

(Stack Team correct me if I am wrong)... ValueConverters do not automatically support in binding and there are reasons... (如果我错了,堆栈团队请纠正我)... ValueConverters不会自动支持绑定,这是有原因的...

  1. They arent really something that the WPF framework is actively aware of, given that they dont lie on visual or logical tree. 考虑到它们不位于视觉或逻辑树上,因此它们确实是WPF框架真正意识到的东西。

  2. They are used as part of inner markup extensions. 它们用作内部标记扩展的一部分。 This is a merky area. 这是一个偏僻的地区。 Unless they implement marrkup extensions on their own, they would be bound to. 除非它们自己实现marrkup扩展,否则它们将受到约束。

Although there are ways.. 虽然有办法..

  1. Straightforward way is to use MultiBinding instead of single binding. 简单的方法是使用MultiBinding而不是单一绑定。 The second binding will replace your converter's need to host a dependncy property. 第二个绑定将取代转换器对托管依赖项属性的需求。

  2. http://www.codeproject.com/KB/WPF/AttachingVirtualBranches.aspx http://www.codeproject.com/KB/WPF/AttachingVirtualBranches.aspx

I hope this helps. 我希望这有帮助。

I think this may be because the ResourceDictionary in which you are creating the instance is not part of the visual tree, so it cannot find the DataContext and the Binding therefore always returns null. 我认为这可能是因为在其中创建实例的ResourceDictionary不属于可视树的一部分,因此它找不到DataContext ,因此Binding始终返回null。

You may be able to get around this by giving your UserControl an x:Name attribute and then binding using ElementName and DataContext.PropertyName : 您可以通过为UserControl提供x:Name属性,然后使用ElementNameDataContext.PropertyName进行绑定来解决此问题:

<UserControl x:Name="Root">
    <UserControl.Resouces>
        <Common:EnumDisplayer x:Key="companyTypes"
                      Localizer="{Binding DataContext.CompanyTypeEnumLocalizer, ElementName=Root}" />
    </UserControl.Resouces>
</UserControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM