通过 CommandParameter 将 2 个硬编码值传递给命令
Passing 2 hardcoded values to a Command through CommandParameter
我正在尝试通过 CommandParameter 将 2 个硬编码参数传递给 DelegateCommand。我目前的委托命令设置如下,因为考虑到限制,它似乎最有意义
private DelegateCommand<Tuple<string, PartImportType>> _mySettingsImportCommandAsync;
public object MyImportCommandAsync
{
get
{
if (_mySettingsImportCommandAsync == null)
{
_mySettingsImportCommandAsync = new DelegateCommand<Tuple<string, PartImportType>>(async partPayload => await OnOpenPartsSelectorCommandAsync(partPayload.Item1, partPayload.Item2));
}
return _mySettingsImportCommandAsync;
}
}
问题变成了如何传递 xaml 中的元组?
我想做这样的事情,但显然这是行不通的
<Button Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Content="Import Settings From DB" Command="{Binding MySettingsImportCommandAsync}" CommandParameter="{system:Tuple<string, PartImportType> ("Event", viewEnums:PartImportType.SimEvent}" Margin="5">
我该怎么做?或者是否有更好的方法将此信息从我的 XAML 获取到我的视图模型?我研究了多重绑定方法,但似乎无法实现,因为我没有绑定到视图中的任何内容...只是试图传递一些硬编码值。
采纳第一个答案中的建议后,我更新后的 XAML 看起来像
<Button Grid.Row="1" Grid.Column="1"
Content="{Binding Part}"
Command="{Binding Path=OpenPartsSelectorCommandAsync}">
<Button.CommandParameter>
<enum:PartImportCommand
Argument="Part"
PartImportType="{x:Static enum:PartImportType.GenericPart}"
Location="Front"/>
</Button.CommandParameter>
</Button>
使用 class 或具有两个 read/write 属性的结构(名称比 Item1 和 Item2 更合适),而不是元组(没有无参数构造函数):
public class MyParam
{
public string Item1 { get; set; }
public PartImportType Item2 { get; set; }
}
并像这样使用它:
<Button ...>
<Button.CommandParameter>
<paramNamespace:MyParam
Item1="Event"
Item2="{x:Static viewEnums:PartImportType.SimEvent}"/>
</Button.CommandParameter>
</Button>
我正在尝试通过 CommandParameter 将 2 个硬编码参数传递给 DelegateCommand。我目前的委托命令设置如下,因为考虑到限制,它似乎最有意义
private DelegateCommand<Tuple<string, PartImportType>> _mySettingsImportCommandAsync;
public object MyImportCommandAsync
{
get
{
if (_mySettingsImportCommandAsync == null)
{
_mySettingsImportCommandAsync = new DelegateCommand<Tuple<string, PartImportType>>(async partPayload => await OnOpenPartsSelectorCommandAsync(partPayload.Item1, partPayload.Item2));
}
return _mySettingsImportCommandAsync;
}
}
问题变成了如何传递 xaml 中的元组? 我想做这样的事情,但显然这是行不通的
<Button Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Content="Import Settings From DB" Command="{Binding MySettingsImportCommandAsync}" CommandParameter="{system:Tuple<string, PartImportType> ("Event", viewEnums:PartImportType.SimEvent}" Margin="5">
我该怎么做?或者是否有更好的方法将此信息从我的 XAML 获取到我的视图模型?我研究了多重绑定方法,但似乎无法实现,因为我没有绑定到视图中的任何内容...只是试图传递一些硬编码值。
采纳第一个答案中的建议后,我更新后的 XAML 看起来像
<Button Grid.Row="1" Grid.Column="1"
Content="{Binding Part}"
Command="{Binding Path=OpenPartsSelectorCommandAsync}">
<Button.CommandParameter>
<enum:PartImportCommand
Argument="Part"
PartImportType="{x:Static enum:PartImportType.GenericPart}"
Location="Front"/>
</Button.CommandParameter>
</Button>
使用 class 或具有两个 read/write 属性的结构(名称比 Item1 和 Item2 更合适),而不是元组(没有无参数构造函数):
public class MyParam
{
public string Item1 { get; set; }
public PartImportType Item2 { get; set; }
}
并像这样使用它:
<Button ...>
<Button.CommandParameter>
<paramNamespace:MyParam
Item1="Event"
Item2="{x:Static viewEnums:PartImportType.SimEvent}"/>
</Button.CommandParameter>
</Button>