WPF CommandParameter 传递多个参数的方法

1、新建一个按钮内容如下

<Button Name="btnOK" 
                            Content="确定" 
                            Height="20"
                            Width="50"
                            Background="#00FF99"  
                            HorizontalAlignment="Left" 
                            Margin="442,30,0,0" 
                            VerticalAlignment="Top" 
                            Command="{Binding ConfirmCommand}"
                            >
                        <Button.CommandParameter>
                            <MultiBinding Converter="{ StaticResource ResourceKey=objectConverter}" Mode="TwoWay">
                                <MultiBinding.Bindings>
                                    <Binding ElementName="dataPickerInts" Path="Datas"/>
                                    <Binding ElementName="dataPickerBytes" Path="Datas"/>
                                </MultiBinding.Bindings>
                            </MultiBinding>
                        </Button.CommandParameter>
                    </Button>

 


2、新建一个类继承:IMultiValueConverter
 public class ObjectConvert : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public static object ConverterObject;       

        public object Convert(object[] values, Type targetType,
          object parameter, System.Globalization.CultureInfo culture)
        {
            ConverterObject = values;
            string str = values.GetType().ToString();   
            return values.ToArray();
        }

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

        #endregion
    }

 


3、最后在主窗口引入
<Window.Resources>
        <local:ObjectConvert x:Key="objectConverter"/>
    </Window.Resources>
posted @ 2022-10-31 09:47  ¥东方不败  阅读(1920)  评论(0)    收藏  举报