您可以使用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)
{
}
}
你不能做到這一點。 –
這可能有助於查看更多的代碼。是'militaryLineAction'和'linesSelector' UI元素嗎? –