/ / WPF Прив'язка CommandParameter до вмісту GridViewColumnHeader з MenuItem - wpf, xaml

WPF Binding CommandParameter для вмісту GridViewColumnHeader з menuItem - wpf, xaml

Я думаю, що це досить конкретне питання, але я намагаюся прив'язати, як CommandParameter в Content a GridViewColumnHeader. Як ви бачите в коді, він працює, коли я роблю це у другому сеттері стилю: <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}" />

Відповіді:

2 для відповіді № 1

Прив'язати Tag власність на PlacementTarget з ContextMenu сама:

<ContextMenu Tag="{Binding PlacementTarget.Content, RelativeSource={RelativeSource Self}}">
<MenuItem CommandParameter="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"

1 для відповіді № 2

Ось що працювало для мене, колега дав мені рішення, мені не вистачало "DataContext", оскільки мій "GridView" фактично в "ListView":

<ContextMenu Tag="{Binding PlacementTarget.CommandParameter, RelativeSource={RelativeSource Self}}"
DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">

Все, що я зробив, це додати a DataContext до ContextMenu. сподіваюся, що це допоможе кожному, хто може мати подібні проблеми.