1. 程式人生 > >wpf mvvm模式下CommandParameter傳遞多參

wpf mvvm模式下CommandParameter傳遞多參

原文: wpf mvvm模式下CommandParameter傳遞多參

CommandParameter一般只允許設定一次,所以如果要傳遞多引數,就要稍微處理一下。我暫時還沒找到更好的方案,下面介紹的這個方案我是目前在用的方案,但給人的感覺總是有些彆扭,不像一個正統的解決方案:

   <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource MultiParamterConverter}">
                    <Binding Path="ID"/>
                    <Binding Path="Name"/>
                </MultiBinding>
            </Button.CommandParameter>

轉換器要這麼寫:

 public class MultiParamterConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //必須新new一個,否則拿不到資料,因為values在返回之後,就會被清空了
            return values.Clone();
        }

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