gpt4 book ai didi

.net - 如何在 XAML 中设置 CommandParameter?

转载 作者:行者123 更新时间:2023-12-01 22:54:22 26 4
gpt4 key购买 nike

我有以下 XAML:

   <dxg:GridControl Name="theGrid" DataSource="{Binding Path=Groupings}">
<dxg:GridControl.ContextMenu>
<ContextMenu>
<MenuItem x:Name="gridprint" Command="{Binding Path=GridPrintCommand}"/>
</ContextMenu>
</dxg:GridControl.ContextMenu>
</dxg:GridControl>

当 GridPrintCommand 执行时,其传递给 GridPrintCommand.Execute(param) 方法的参数为空。我想将参数设置为网格控件。我可以像这样在代码隐藏中做到这一点:

gridprint.CommandParameter = theGrid;

这很好用。但是我试图在 XAML 中设置相同的东西 (CommandParameter),但我就是做不对!我输入的所有内容都会导致 BindingExpressionError 或 null 参数。我错过了什么?

最佳答案

尝试使用这个:

<MenuItem x:Name="gridprint"
CommandParameter="{Binding PlacementTarget, RelativeSource=
{RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />

ContextMenus 不是 WPF 的普通 VisualTree 的一部分,因此您必须使用 PlacementTarget 进行绑定(bind)以获取 ContextMenu 附加到的对象

关于.net - 如何在 XAML 中设置 CommandParameter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5235809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com