MVVM中轻松实现Command绑定 传递Command参数 ConverterParameter

10 篇文章 12 订阅

wpf 绑定Command 传递CommandParameter参数

<Window xmlns:vm="clr-namespace:WpfDemo>
<Window.DataContext>
	<vm:Window1ViewModel />
</Window.DataContext>
<Button Command="{Binding LoginCommand}" CommandParameter="20" />
也可以传递控件给CommandParameter
注意{Binding RelativeSource={x:Static RelativeSource.Self}}是绑定自己(Button)的意思。
<Button Command="{Binding LoginCommand1}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" />
</Window>

namespace WpfDemo
{
	public class Window1ViewModel
	{
		public ICommand LoginCommand
		{
			get
			{
				return new DelegateCommand<sring>(str=>{
					MessageBox.Show(str);					
				});
			}
		}
		
		public ICommand LoginCommand1
		{
			get
			{
				return new DelegateCommand<sring>(str=>{
					MessageBox.Show(str);					
				});
			}
		}
	}
}

wpf converter coverterparameter ivalueconverter imultivalueconverter
1、converterparameter不是依赖属性,所以不能用binding
2、可以把converter的接口由ivaluecovneter改为imultivalueconverter,实现多个绑定
3、imultivalueconverter例子

public class DateToCConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null)
                return null;
            return null;
        }

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


xaml写法

 <TextBlock   Foreground="{TemplateBinding Foreground}"  >
     <TextBlock.Text>
         <MultiBinding Converter="{StaticResource DateToCConverter}">
             <Binding Path="Content" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=Control}"/>
             <Binding Path="CurrentYear" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=src:MyCustomerControlType}" />
         </MultiBinding>
     </TextBlock.Text>
 </TextBlock>


或者
<TextBlock Text="{Binding Path=Text,ElementName=TextBox,Converter={StaticResource MultiConverter},ConverterParameter=10}" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值