• Binding ConvererParameter


    WPF中ConverterParameter不可以绑定,可以通过如下扩展来实现ConverterParameter的Binding:

    1.自定义ConverterBindableBinding和MultiValueConverterAdapter:

    namespace TTSControl.UtilityClasses
    {
        public class ConverterBindableBinding : MarkupExtension
        {
            public Binding Binding { get; set; }

            public IValueConverter Converter { get; set; }

            public Binding ConverterParameterBinding { get; set; }

            public Binding ConverterBinding { get; set; }

            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                MultiBinding multiBinding = new MultiBinding();
                multiBinding.Bindings.Add(Binding);
                multiBinding.Bindings.Add(ConverterParameterBinding);
                if (ConverterBinding != null) multiBinding.Bindings.Add(ConverterBinding);
                MultiValueConverterAdapter adapter = new MultiValueConverterAdapter();
                adapter.Converter = Converter;
                multiBinding.Converter = adapter;
                return multiBinding.ProvideValue(serviceProvider);
            }
        }
    }

    namespace TTSControl.UtilityClasses
    {
        [ContentProperty("Converter")]
        public class MultiValueConverterAdapter : IMultiValueConverter
        {
            public IValueConverter Converter { get; set; }

            #region IMultiValueConverter Members

            private object lastParameter;
            private IValueConverter lastConverter;

            public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                lastConverter = Converter;
                if (values.Length > 1) lastParameter = values[1];
                if (values.Length > 2) lastConverter = (IValueConverter)values[2];
                if (Converter == null) return values[0];
                return Converter.Convert(values[0], targetType, lastParameter, culture);
            }

            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                if (lastConverter == null) return new object[] { value };
                return new object[] { lastConverter.ConvertBack(value, targetTypes[0], lastParameter, culture) };
            }

            #endregion

        }
    }

    2。在xaml中绑定:

    xmlns:uti="clr-namespace:TTSControl.UtilityClasses"        

     
     Binding="{Binding PermissionKey}" Converter="{StaticResource permissionValueConverter}"ConverterParameterBinding="{Binding UserName}" />
     

    3。在自定义Converter类中获取绑定数据:

        internal class PermissionValueConverter : DependencyObject, IValueConverter, IMultiValueConverter
        {
           
            // For testing of converter properties, we derice from DependencyObject and add a dependency property.

            public String UserName
            {
                get { return (String)GetValue(UserNameProperty); }
                set { SetValue(UserNameProperty, value); }
            }

            // Using a DependencyProperty as the backing store for ConcatSign.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty UserNameProperty =
                DependencyProperty.Register("UserName", typeof(String), typeof(PermissionValueConverter), new UIPropertyMetadata(null));

            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string permissionKey = (string)value;
                string  userName= (string)parameter;

                List> PermissionValues = new List>();
                PermissionValues = Database.GetPermissionValue(Properties.Settings.Default.gLanguage, userName, permissionKey);
                return PermissionValues;
      
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }

            #endregion

            #region IMultiValueConverter Members

            public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return Convert(values[0], targetType, values[1], culture);
            }

            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                return new object[] { ConvertBack(value, targetTypes[0], parameter, culture) };
            }

            #endregion

        }

  • 相关阅读:
    Android Studio 快捷键(包含自定义)终极版
    C#开发模式——dll多级引用的问题
    WPF实现QQ群文件列表动画(二)
    WPF实现QQ群文件列表动画(一)
    VS2010Datatable查看器查看超时(Microsoft.VisualStudio.DebuggerVisualizers)
    WPF里ItemsControl的分组实现
    WPF触控程序开发(四)——MultiTouchVista_-_second_release_-_refresh_2的救赎
    WPF异步回调时回调函数如何获取异步函数产生的变量
    裸奔着造房子——对政府禁止采购Win8系统的一些看法
    全球征集-如何实现回文SQL的查询
  • 原文地址:https://www.cnblogs.com/eric_ibm/p/3784299.html
Copyright © 2020-2023  润新知