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}" />