绑定转换器内部类? [英] Binding converter as inner class?

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

问题描述

我有一个使用绑定转换器用户控件。我做了该转换器的内部类的

 公共部分类MyPanel:用户控件
{
    公共类CornerRadiusConverter:的IValueConverter
    {
 

我如何从XAML引用转换器类?下面不工作:

 <控制:MyPanel.CornerRadiusConverter X:关键=CornerRadiusConverter/>
 

这给出了这样的错误:

  

标签   LensPanel.CornerRadiusConverter'呢   在XML命名空间不存在   CLR命名空间:MyApp.Windows.Controls

解决方案

我在想这个问题又来了,我想出了类似丹尼斯的解决方案的东西:创建一个代理转换器类,用类属性,将创建实际的转换器的实例,并转化委托给它。

 公共类转换器:的IValueConverter
{
    私有类型_type = NULL;
    公共类型类型
    {
        {返回_type; }
        组
        {
            如果(值!= _type)
            {
                如果(value.GetInterface(的IValueConverter)!= NULL)
                {
                    _type =价值;
                    _CONVERTER = NULL;
                }
                其他
                {
                    抛出新的ArgumentException(
                        的String.Format(类型{0}未实现的IValueConverter,value.FullName)
                        值);
                }
            }
        }
    }

    私人的IValueConverter _CONVERTER = NULL;
    私人无效CreateConverter()
    {
        如果(_CONVERTER == NULL)
        {
            如果(_type!= NULL)
            {
                _CONVERTER = Activator.CreateInstance(_type)作为的IValueConverter;
            }
            其他
            {
                抛出新的InvalidOperationException异常(转换器类型没有定义);
            }
        }
    }

    #地区的IValueConverter会员

    公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
    {
        CreateConverter();
        返回_converter.Convert(值,TARGETTYPE,参数,文化);
    }

    公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
    {
        CreateConverter();
        返回_converter.ConvertBack(值,TARGETTYPE,参数,文化);
    }

    #endregion
}
 

您使用它这样的:

 < Window.Resources>
    <我:转换器X:关键=CornerRadiusConverter类型={X:类型的控件:MyPanel + CornerRadiusConverter}/>
< /Window.Resources>
 

I have a UserControl that uses a binding converter. I've made the converter an inner class of

public partial class MyPanel : UserControl
{
    public class CornerRadiusConverter : IValueConverter
    {

How do I reference the Converter class from the XAML? The following does not work:

<controls:MyPanel.CornerRadiusConverter x:Key="CornerRadiusConverter" />

It gives this error:

The tag 'LensPanel.CornerRadiusConverter' does not exist in XML namespace 'clr-namespace:MyApp.Windows.Controls'

解决方案

I was thinking about this problem again, and I came up with something similar to Dennis's solution : create a "proxy" converter class, with a Type property, which will create the instance of the actual converter and delegate the conversion to it.

public class Converter : IValueConverter
{
    private Type _type = null;
    public Type Type
    {
        get { return _type; }
        set
        {
            if (value != _type)
            {
                if (value.GetInterface("IValueConverter") != null)
                {
                    _type = value;
                    _converter = null;
                }
                else
                {
                    throw new ArgumentException(
                        string.Format("Type {0} doesn't implement IValueConverter", value.FullName),
                        "value");
                }
            }
        }
    }

    private IValueConverter _converter = null;
    private void CreateConverter()
    {
        if (_converter == null)
        {
            if (_type != null)
            {
                _converter = Activator.CreateInstance(_type) as IValueConverter;
            }
            else
            {
                throw new InvalidOperationException("Converter type is not defined");
            }
        }
    }

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        CreateConverter();
        return _converter.Convert(value, targetType, parameter, culture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        CreateConverter();
        return _converter.ConvertBack(value, targetType, parameter, culture);
    }

    #endregion
}

You use it like that :

<Window.Resources>
    <my:Converter x:Key="CornerRadiusConverter" Type="{x:Type controls:MyPanel+CornerRadiusConverter}"/>
</Window.Resources>

这篇关于绑定转换器内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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