wpf mvvm模式下CommandParameter传递多参
CommandParameter一般只允許設(shè)置一次,所以如果要傳遞多參數(shù),就要稍微處理一下。我暫時(shí)還沒(méi)找到更好的方案,下面介紹的這個(gè)方案我是目前在用的方案,但給人的感覺(jué)總是有些別扭,不像一個(gè)正統(tǒng)的解決方案:
?? <Button.CommandParameter>
??????????????? <MultiBinding Converter="{StaticResource MultiParamterConverter}">
??????????????????? <Binding Path="ID"/>
??????????????????? <Binding Path="Name"/>
??????????????? </MultiBinding>
??????????? </Button.CommandParameter>
轉(zhuǎn)換器要這么寫(xiě):
?public class MultiParamterConverter : IMultiValueConverter
??? {
??????? public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
??????? {
??????????? //必須新new一個(gè),否則拿不到數(shù)據(jù),因?yàn)関alues在返回之后,就會(huì)被清空了
??????????? return values.Clone();
??????? }
??????? public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
??????? {
??????????? throw new NotImplementedException();
??????? }
??? }
轉(zhuǎn)載于:https://www.cnblogs.com/lonelyxmas/p/10147181.html
總結(jié)
以上是生活随笔為你收集整理的wpf mvvm模式下CommandParameter传递多参的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 中介者模式(Mediator Patte
- 下一篇: DevExpress WPF v18.2