Binding converter as inner class?

前端 未结 3 500
太阳男子
太阳男子 2020-12-19 04:52

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          


        
3条回答
  •  无人及你
    2020-12-19 05:30

    It could be possible. A few months ago I wrote a markup extension to create the converter for you inline. It keeps a dictionary of weak references so that you don't create multiple instances of the same converter. Handles creating converters with different arguments too.

    In XAML:

    
    

    C#:

    [MarkupExtensionReturnType(typeof(IValueConverter))]
    public class InlineConverterExtension : MarkupExtension
    {
      static Dictionary s_WeakReferenceLookup;
    
      Type m_ConverterType;
      object[] m_Arguments;
    
      static InlineConverterExtension()
      {
        s_WeakReferenceLookup = new Dictionary();
      }
    
      public InlineConverterExtension()
      {
      }
    
      public InlineConverterExtension(Type converterType)
      {
        m_ConverterType = converterType;
      }
    
      /// 
      /// The type of the converter to create
      /// 
      /// The type of the converter.
      public Type ConverterType
      {
        get { return m_ConverterType; }
        set { m_ConverterType = value; }
      }
    
      /// 
      /// The optional arguments for the converter's constructor.
      /// 
      /// The argumments.
      public object[] Arguments
      {
        get { return m_Arguments; }
        set { m_Arguments = value; }
      }
    
      public override object ProvideValue(IServiceProvider serviceProvider)
      {
        IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
    
        PropertyInfo propertyInfo = target.TargetProperty as PropertyInfo;
    
        if (!propertyInfo.PropertyType.IsAssignableFrom(typeof(IValueConverter)))
          throw new NotSupportedException("Property '" + propertyInfo.Name + "' is not assignable from IValueConverter.");
    
        System.Diagnostics.Debug.Assert(m_ConverterType != null, "ConverterType is has not been set, ConverterType{x:Type converterType}");
    
        try
        {
          string key = m_ConverterType.ToString();
    
          if (m_Arguments != null)
          {
            List args = new List();
            foreach (object obj in m_Arguments)
              args.Add(obj.ToString());
    
            key = String.Concat(key, "_", String.Join("|", args.ToArray()));
          }
    
          WeakReference wr = null;
          if (s_WeakReferenceLookup.TryGetValue(key, out wr))
          {
            if (wr.IsAlive)
              return wr.Target;
            else
              s_WeakReferenceLookup.Remove(key);
          }
    
          object converter = (m_Arguments == null) ? Activator.CreateInstance(m_ConverterType) : Activator.CreateInstance(m_ConverterType, m_Arguments);
          s_WeakReferenceLookup.Add(key, new WeakReference(converter));
    
          return converter;
        }
        catch(MissingMethodException)
        {
          // constructor for the converter does not exist!
          throw;
        }
      }
    
    }
    

提交回复
热议问题