我正在尝试使用自定义类在XAML中传递多个CommandParameters。
我创建了一个名为ValueCommandArgs的类,它继承自DependencyObject并拥有两个依赖属性(我们在这个例子中称它们为Value1和Value2)。
调用命令并传递此对象的按钮如下所示:
<Button Command="{Binding ChangeValueCommand}" Content="Execute Command">
<Button.CommandParameter>
<args:ValueCommandArgs Value1="{Binding TestValue1}" Value2="{Binding TestValue2}" />
</Button.CommandParameter>
</Button>
我的命令参数中有一个ValueCommandArgs对象,然而属性Value1和Value2始终为空。
我知道可以使用MultiBinding和Converter来解决这个问题,但我认为我尝试的方法会更加简洁明了。
为什么这样不起作用?
DataContext
被传递到ValueCommandArgs
,但我认为它可能没有被传递。我也不知道如何实现一些类型来接收传递给它的DataContext
。在这种情况下,您应该明确设置绑定的Source
。(请注意,如果您的ValueCommandArgs
继承自FrameworkElement
或FrameworkContentElement
,则ElementName
和RelativeSource
可用)。 - King King