2017-07-17 102 views
2

我想這是一個非常具體的問題,但我想綁定得到CommandParameterContentGridViewColumnHeader。正如你將會在代碼中看到的那樣,當我在樣式的第二個設置器中執行時,它會工作:<Setter Property="CommandParameter" Value="{Binding Content, RelativeSource={RelativeSource Self}}"/>。但它對我的菜單項不起作用,我該如何綁定它們?下面是代碼:WPF綁定CommandParameter到GridViewColumnHeader內容從MenuItem

<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}" /> 

回答

1

這裏是我工作,一個同事給我的解決方案,我失蹤了「 DataContext的」爲我 '的GridView' 是actualy在 'ListView控件':

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

我所做的就是添加一個DataContextContextMenu。希望這可以幫助任何可能有類似問題的人。

2

綁定Tag屬性爲ContextMenu本身的PlacementTarget

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

謝謝,這只是其中一個問題。一位同事給了我解決方案,我錯過了'DataContext',因爲我的'GridView'實際上是在'ListView'中。 –

+0

那麼,關鍵還是要綁定到ComboBox的PlacementTarget。是否應該綁定到PlacementTarget的DataContext是另一回事。 – mm8