Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

c# - Binding converter as inner class?

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'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...