通过Converter或ExtensionMethod读取属性属性

时间:2014-04-02 06:02:28

标签: .net wpf xaml mvvm localization

我正在实现一个自定义本地化系统,我使用属性属性来存储本地化文本。 我需要做的是使用DisplayName属性中包含的文本设置Textblock的文本。 例如,我有这个属性:

[DisplayName("First name")] public string FirstName { get; set; }

在XAML中我需要做类似

的事情

<Textblock Text={mvvm:Localize FirstName} />

<Textblock Text={Binding FirstName, Converter={Staticresource DisplayNameReader}} />

但我找不到达到属性的方法,因为转换器只知道属性的值,属性的公开类的类型和参数。

我尝试使用扩展方法,执行类似

的操作

<Textblock Text={mvvm:Localize {Binding FirstName}} />

为了将绑定传递给属性,但我得到的只是一个带空源的绑定。

你可以帮帮我吗? 谢谢你们!

编辑:这是我的标记扩展程序

public class DisplayDescriptionExtension : MarkupExtension
{
    public DisplayDescriptionExtension() { }

    public DisplayDescriptionExtension(Binding binding)
    {
        this.Binding = binding;
    }

    [ConstructorArgument("binding")]
    public Binding Binding { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (this.Binding == null ||
            this.Binding.Source == null)
            return string.Empty;

        var propertyInfo = piSource.GetType().GetProperty(step);

        var displayAtts = Attribute.GetCustomAttributes(propertyInfo, typeof(DisplayAttribute), true);

        if (displayAtts != null)
            return (displayAtts[0] as DisplayAttribute).Description;

        return string.Empty;
    }
}

问题是第一个if总是满足,因为源是null。

2 个答案:

答案 0 :(得分:2)

解读this article

null源的问题与Markup Extension在解析时只被评估一次的事实有关,因此View在那时没有它的DataContext。 解决方案是订阅一个事件,当值准备好被扩展读取时,该事件会引发。

这是我绑定到属性属性的解决方案:

用法:

<TextBlock Text="{local:DisplayDescription Binding={Binding PropertyName}}" />

代码:

public abstract class UpdatableMarkupExtension : MarkupExtension
{
    protected object TargetObject { get; private set; }
    protected object TargetProperty { get; private set; }

    public sealed override object ProvideValue(IServiceProvider serviceProvider)
    {
        IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (target != null)
        {
            this.TargetObject = target.TargetObject;
            this.TargetProperty = target.TargetProperty;
        }

        this.Subscribe();

        return ProvideValueInternal(serviceProvider);
    }

    protected void UpdateValue(object value)
    {
        if (this.TargetObject != null)
        {
            if (this.TargetProperty is DependencyProperty)
            {
                DependencyObject obj = this.TargetObject as DependencyObject;
                DependencyProperty prop = this.TargetProperty as DependencyProperty;

                Action updateAction = () => obj.SetValue(prop, value);

                if (obj.CheckAccess())
                    updateAction();
                else
                    obj.Dispatcher.Invoke(updateAction);
            }
            else
            {
                PropertyInfo prop = this.TargetProperty as PropertyInfo;
                prop.SetValue(this.TargetObject, value, null);
            }
        }
    }

    protected abstract void Subscribe();

    protected abstract object ProvideValueInternal(IServiceProvider serviceProvider);
}

[MarkupExtensionReturnType(typeof(string))]
public class DisplayDescriptionExtension : UpdatableMarkupExtension
{
    public DisplayDescriptionExtension()
    {
    }

    public DisplayDescriptionExtension(Binding binding)
    {
        this.Binding = binding;
    }

    [ConstructorArgument("binding")]
    public Binding Binding { get; set; }

    void DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var pi = (sender as FrameworkElement).DataContext.GetType().GetProperty(this.Binding.Path.Path);

        var displayAtt = pi.GetCustomAttribute<DisplayAttribute>(true);

        var displayName = displayAtt != null ? displayAtt.Description : string.Empty;

        this.UpdateValue(displayName);
    }

    protected override object ProvideValueInternal(IServiceProvider serviceProvider)
    {
        return "!";
    }

    protected override void Subscribe()
    {
        (this.TargetObject as FrameworkElement).DataContextChanged += DataContextChanged;
    }
}

绑定的使用确保它引用了不动产,因此我可以在设计时注意到任何拼写错误。

仍然需要扩展PropertyPath逻辑,因为它仅适用于第一级属性。

非常感谢 Willem van Rumpt 的时间和想法。

答案 1 :(得分:1)

扩展您的MarkupExtension,下面的代码示例应该适合您。您可以为标记扩展指定属性路径和类型或对象,然后您可以找到您感兴趣的成员。

编辑:

如果您想引用它,我已将原始解决方案(突破)保留在新解决方案之下。还有一个选项可以使用类似的方法作为Binding扩展,但它更复杂,依赖于反射来创建BindingExpression,你仍然需要使用DependencyObjects,不太清楚,并且,恕我直言,不值得的麻烦。另外,我还没有一个有效的例子;)。

如果您允许语言的运行时切换,那么调查该路由可能会很有趣,在这种情况下,实际绑定可能值得您花时间。

public class DisplayDescriptionExtension : MarkupExtension
{
    public DisplayDescriptionExtension() { }
    public DisplayDescriptionExtension(string propertyPath)
    {
        PropertyPath = propertyPath;
    }

    [ConstructorArgument("propertyPath")]
    public string PropertyPath { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (serviceProvider == null)
        {
            return null;
        }

        if (string.IsNullOrEmpty(PropertyPath))
        {
            return null;
        }

        IProvideValueTarget pvt = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (pvt == null)
        {
            return null;
        }

        FrameworkElement d = pvt.TargetObject as FrameworkElement;
        if (d == null)
        {
            return null;
        }

        object context = d.DataContext;
        if (context == null)
        {
            return null;
        }

        object returnValue = null;
        ///* ToDo:
        // * Using "PropertyPath" and context, find the member you're interested in, and 
        // * fill returnValue approriately. Keep in mind that "PropertyPath" may contain nested properties! 
        // */

        return returnValue;
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Height="350"
        Width="525"
        >
    <Window.DataContext>
        <l:ViewModel x:Name="TheViewModel" />
    </Window.DataContext>
    <Grid x:Name="Root">
        <Button Content="{l:DisplayDescription Value}" />
    </Grid>
</Window>

旧解决方案

<击>     public class DisplayDescriptionExtension:MarkupExtension     {         public DisplayDescriptionExtension(){}

    public DisplayDescriptionExtension(string propertyPath, object context)
    {
        PropertyPath = propertyPath;
        Context = context;
    }

    public DisplayDescriptionExtension(string propertyPath, Type contextType)
    {
        PropertyPath = propertyPath;
        ContextType = contextType;
    }

    [ConstructorArgument("propertyPath")]
    public string PropertyPath { get; set; }

    [ConstructorArgument("context")]
    public object Context { get; set; }

    [ConstructorArgument("contextType")]
    public Type ContextType { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (serviceProvider == null)
        {
            return null;
        }

        if (string.IsNullOrEmpty(PropertyPath))
        {
            return null;
        }

        Type contextType = ContextType;
        if (contextType == null)
        {
            if (Context == null)
            {
                return null;
            }

            contextType = Context.GetType();
        }

        object returnValue = null;

        /* ToDo:
         * Using "PropertyPath" and contextType, find the member you're interested in, and 
         * fill returnValue approriately. Keep in mind that "PropertyPath" may contain nested properties! 
         */

        return returnValue;
    }
}

用法:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Window.DataContext>
        <l:ViewModel x:Name="TheViewModel" />
    </Window.DataContext>
    <Grid>
        <Grid.Resources>
            <l:ValueConverter x:Key="converter" />
        </Grid.Resources>
        <!-- Using an object reference -->
        <Button Content="{l:DisplayDescriptionExtension MyProperty.NestedProperty, {x:Reference Name=TheViewModel}}" />
        <!-- Using a Type -->
        <Button Content="{l:DisplayDescriptionExtension OtherProperty, {x:Type l:ViewModel}}" />
    </Grid>
</Window>

<击>