我想这是一个非常具体的问题,但我试图CommandParameter
GridViewColumnHeader
的Content
绑定get。正如您将在代码中看到的那样,当我在样式的第二个二传器中执行此操作时,它可以工作:<Setter Property="CommandParameter" Value="{Binding Content, RelativeSource={RelativeSource Self}}"/>
.但它不适用于我的菜单项,我如何绑定它们?这是代码:
<Style BasedOn="{StaticResource {x:Type GridViewColumnHeader}}" TargetType="GridViewColumnHeader">
<Setter Property="Command" Value="{Binding SortBy}" />
<Setter Property="CommandParameter" Value="{Binding Content, RelativeSource={RelativeSource Self}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu Tag="{Binding Content, RelativeSource={RelativeSource AncestorType=GridViewColumnHeader}}">
<MenuItem CommandParameter="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
Header="{UI:Language @{SortAscending}}"
Command="{Binding SortAscending}" />
将Tag
属性绑定到ContextMenu
本身的PlacementTarget
:
<ContextMenu Tag="{Binding PlacementTarget.Content, RelativeSource={RelativeSource Self}}">
<MenuItem CommandParameter="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
这是对我有用的方法,一位同事给了我解决方案,我缺少"数据上下文",因为我的"网格视图"实际上在"列表视图"中:
<ContextMenu Tag="{Binding PlacementTarget.CommandParameter, RelativeSource={RelativeSource Self}}"
DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
我所做的只是在ContextMenu
中添加一个DataContext
。 希望这对任何可能有类似问题的人有所帮助。