我想这是一个非常具体的问题,但我试图将CommandParameter
与Content
GridViewColumnHeader
绑定起来。正如您将在代码中看到的那样,当我在样式的第二个setter中执行它时它会起作用:<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}" />
答案 0 :(得分:2)
将Tag
属性绑定到PlacementTarget
本身的ContextMenu
:
<ContextMenu Tag="{Binding PlacementTarget.Content, RelativeSource={RelativeSource Self}}">
<MenuItem CommandParameter="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
答案 1 :(得分:1)
这对我有用,一位同事给了我解决方案,我错过了一个&#39; DataContext&#39;作为我的GridView&#39;实际上是在ListView&#39;:
<ContextMenu Tag="{Binding PlacementTarget.CommandParameter, RelativeSource={RelativeSource Self}}"
DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
我所做的只是向DataContext
添加ContextMenu
。希望这有助于任何可能遇到类似问题的人。