绑定到 WPF 中的扩展方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18484123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:31:28  来源:igfitidea点击:

Bind to extension method in WPF

c#wpfbindingextension-methods

提问by John Smith

I have a simple class in C#:

我在 C# 中有一个简单的类:

public class Dog {
    public int Age {get; set;}
    public string Name {get; set;}
}

and I created an extension Method like this :

我创建了一个像这样的扩展方法:

public static class DogExt {
    public static string BarkYourName(this Dog dog) {
        return dog.Name + "!!!";
    }
}

Is there any way how to bind result of the BarkYourNamemethod to a wpf component?

有什么方法可以将BarkYourName方法的结果绑定到 wpf 组件?

Basically : is there any way hwo to bind it to an extension method?

基本上:有什么办法可以将它绑定到扩展方法?

回答by Spontifixus

No, you cannot bind to an extension method. You can bind to the Name-property of the Dog-object and use a converter though.

不,您不能绑定到扩展方法。您可以绑定到Name-object 的 -propertyDog并使用转换器。

To create a converter create a class implementing the IValueConverterinterface. You will need a one-way conversion only (from the model to the view) so will need to implement the Convertmethod only. The ConvertBackmethod is not supported by your converter and thus throws a NotSupportedException.

要创建转换器,请创建一个实现IValueConverter接口的类。您只需要单向转换(从模型到视图),因此Convert只需要实现该方法。ConvertBack您的转换器不支持该方法,因此会抛出NotSupportedException.

public class NameToBarkConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dog = value as Dog;
        if (dog != null)
        {
            return dog.BarkYourName();
        }
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Then you can use your converter as follows:

然后您可以按如下方式使用您的转换器:

<Grid>
    <Grid.Resources>
        <NameToBarkConverter x:Key="NameToBarkConverter" />
    </Grid.Resources>
    <TextBlock Text="{Binding Name, Converter={StaticResource NameToBarkConverter}}" />
</Grid>

For more information on converters see MSDN: IValueConverter.

有关转换器的更多信息,请参阅MSDN: IValueConverter

回答by JSJ

Programming is the logical IQ game.

编程是逻辑智商游戏。

Binding with MethodName is not possible in WPF as the Binding engine only works with Properties. Although, you can use some tricks.

在 WPF 中无法使用 MethodName 进行绑定,因为绑定引擎仅适用于属性。虽然,你可以使用一些技巧。

Here are some ways to accomplish this.

这里有一些方法可以实现这一点。

  1. Add an extension method to the getaccessor of the property, like this

    private string _name;
    public string Name
    {
        get { return _name.BarkYourName(); }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }
    
  2. Add a converter to change the effect of the Name property, like this

    public class NameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "!!!";
    
            return value.ToString().BarkYourName();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    public static class StringExt
    {
        public static string BarkYourName(this string str)
        {
            return str + "!!!";
        }
    }
    
  1. get属性的访问器添加一个扩展方法,像这样

    private string _name;
    public string Name
    {
        get { return _name.BarkYourName(); }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }
    
  2. 添加转换器改变Name属性的效果,像这样

    public class NameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "!!!";
    
            return value.ToString().BarkYourName();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    public static class StringExt
    {
        public static string BarkYourName(this string str)
        {
            return str + "!!!";
        }
    }
    

回答by Nitin

No way to bind to methods. You can create the property in Dogclass like below and bind to it

无法绑定到方法。您可以在Dog类中创建属性,如下所示并绑定到它

public string NameEx
    {
        get
        {
            return this.BarkYourName();
        }
    }

and if you want to keep Nameand NameExin sync, just raise the PropertyChanged-event for NameExalso in the setter of the Name-property.

如果你想保持NameNameEx同步,只是提高PropertyChanged-event为NameEx还在的制定者Name-属性。