2011-08-08 74 views
0

我希望在基於WPF的桌面應用程序的網格視圖中有兩個上下文菜單。 目前我可以顯示一個上下文菜單,但我想在一個條件上顯示上下文菜單1,在另一個條件上顯示上下文菜單2。怎麼做?在WPF網格行中顯示多個上下文菜單

我usign以下XAML代碼顯示網格和上下文菜單

<dg:UCGrid x:Name="grdLetVariables" Grid.Row="2" GridTypeSource="LetGrid" 
               DataContext="{Binding}" > 
           <dg:UCGrid.Resources> 
            <x:Array Type="{x:Type sys:Object}" x:Key="GridExtensions"> 
             <MenuItem Header="Delete" Click="ContextMenuDelete"> 
              <MenuItem.Icon> 
               <Image Height="10" Source="../images/Delete.png"/> 
              </MenuItem.Icon> 
             </MenuItem> 
             <Separator /> 
             <MenuItem Header="Move Up" Click="MoveUpLetGrdRow"> 
              <MenuItem.Icon> 
               <Image Height="14" Source="../images/UpMove.png"/> 
              </MenuItem.Icon> 
             </MenuItem> 
             <MenuItem Header="Move Down" Click="MoveDownLetGrdRow"> 
              <MenuItem.Icon> 
               <Image Height="14" Source="../images/DownMove.png"/> 
              </MenuItem.Icon> 
             </MenuItem> 
             <Separator /> 
             <MenuItem Header="Cancel" Click="CancelLetGrdRowEdit"/> 
            </x:Array> 
           </dg:UCGrid.Resources> 
           <dg:UCGrid.ContextMenu> 
            <ContextMenu> 
             <ContextMenu.ItemsSource> 
              <CompositeCollection> 
               <CollectionContainer Collection="{StaticResource GridExtensions}" /> 
              </CompositeCollection> 
             </ContextMenu.ItemsSource> 
            </ContextMenu> 
           </dg:UCGrid.ContextMenu> 
          </dg:UCGrid> 
         </Grid> 

回答

3

觸發器在數據網格可以幫助你在這裏。下面的代碼僅用於說明...

<UserContorl.Resources> 
    <ContextMenu x:Key="Condition1ContextMenu" ../> 
    <ContextMenu x:Key="Condition2ContextMenu" ../> 
</UserControl.Resources> 
... 
<Style TargetType="{x:Type dg:UCGrid}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Condition1}" Value="Value1"> 
      <Setter Property="ContextMenu" Value="{StaticResource Condition1ContextMenu}"/> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding Condition2}" Value="Value2"> 
      <Setter Property="ContextMenu" Value="{StaticResource Condition2ContextMenu}"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

當然條件1和條件2必須是彼此排斥的。如果它們都適用於數據網格,那麼由於訂單Condition2ContextMenu將優先。

讓我知道如果這能幫助...

+0

我有一個類似的問題..除了那些ContextMenus數據綁定,當我點擊菜單項不起作用。任何想法是怎麼回事? – reservoirman

相關問題