![](/img/trans.png)
[英]How to Pass HashCode of a Control as a CommandParameter in WPF XAML
[英]Pass multi parameter to CommandParameter in xaml
我想通过xaml中的commandparameter将serval参数传递给命令。
<i:InvokeCommandAction Command="{Binding HideLineCommand, ElementName=militaryLineAction}"
CommandParameter="{Binding ID, ElementName=linesSelector}"/>
在上面的示例中,我想将其他变量传递给ID变量旁边的命令。 我该如何实现? 万分谢意。
您可以将MultiBinding与转换器一起使用。
检查此示例。
假设您有Person类。
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
并且您想要此类作为您的命令参数。
您的XAML应该如下所示:
<Button Content="Start"
DataContext="{Binding SourceData}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding SendStatus, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource myPersonConverter}">
<MultiBinding.Bindings>
<Binding Path="Name" />
<Binding Path="Age" />
</MultiBinding.Bindings>
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
其中SourceData
是一个Person对象。
myPersonConverter
是一个PersonConverter对象。
public class PersonConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values != null && values.Length == 2)
{
string name = values[0].ToString();
int age = (int)values[1];
return new Person { Name = name, Age = age };
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在命令中,您可以使用Person对象作为参数:
public ICommand SendStatus { get; private set; }
private void OnSendStatus(object param)
{
Person p = param as Person;
if (p != null)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.