2011-03-10 111 views
1

我在wpf中有一個上下文菜單。菜單中的一個項目有一個子菜單,它從標題菜單項的ItemsSource中填充。這個子菜單是可以發送到應用程序的另一部分的命令列表。該列表基本上是限於10個項目的mru列表。我想添加一個分隔符,然後在10個項目列表下面添加一個「More」選項,以便用戶可以看到可用命令的整個列表。我似乎無法弄清楚如何添加這些額外的項目。我可以得到列表從父菜單項的ItemsSource動態填充,但我似乎無法弄清楚如何將其他項添加到子菜單的底部。我不想將它們放在項目源中,「更多」項目需要有自己的命令。上下文子菜單項目來源和附加項目

<MenuItem x:Name="ExecuteCommandMenuItem" Height="22" Style="{StaticResource RightClickMenuItemStyle}" 
          ItemsSource="{Binding Path=PanelCommands}"> 
        <MenuItem.Header> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Text="Panel Command" HorizontalAlignment="Left" Width="100"/> 
         </StackPanel> 
        </MenuItem.Header> 

        <MenuItem.ItemContainerStyle> 
         <Style TargetType="MenuItem" BasedOn="{StaticResource RightClickMenuItemStyle}"> 
          <Setter Property="MenuItem.Header" Value="{Binding}" /> 
          <Setter Property="MenuItem.Command" Value="CommonCommands:CommandRepository.ExecutePanelCommand" /> 
          <Setter Property="MenuItem.CommandParameter"> 
           <Setter.Value> 
            <MultiBinding Converter="{CommonConverter:PanelCommandArgsConverter}"> 
             <MultiBinding.Bindings> 
              <Binding Path="DataContext" RelativeSource="{RelativeSource FindAncestor, 
                  AncestorType={x:Type ContextMenu}}"/> 
              <Binding Path="Command" /> 
             </MultiBinding.Bindings> 
            </MultiBinding> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </MenuItem.ItemContainerStyle> 
       </MenuItem> 

謝謝。

+0

的可能重複[如何動態綁定和靜態添加的MenuItems?](http://stackoverflow.com/questions/14489112/how-do-i-dynamically-bind-and-靜態添加菜單項) – GarethD 2013-10-28 12:59:52

回答

0

我不認爲你可以,因爲它是綁定到源。所以要麼將它們添加到源代碼中,要麼使用模板選擇器並在那裏執行你的邏輯。定義一個普通模板,然後定義一個「更多」模板。

或者,你可以做一些控制築巢一樣

<menu> 
<stackpanel> 
<Menu Items> 

</menu Items> 
<break /> 
<Button>More</button> 
</stackpanel> 
</menu> 

對不起,這只是從我的頭頂。你可以發佈你的XAML嗎?

+0

謝謝。我添加了我的xaml。我會嘗試你的建議。 – Ben 2011-03-11 21:38:03

2
<DataGrid x:Class="UICCNET.BaseControls.UserControls.BaseDataGrid" 
     Tag="{Binding RelativeSource={RelativeSource Self}, Path=Columns}"> 
<DataGrid.ContextMenu> 
    <ContextMenu Tag="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag}"> 
     <MenuItem Header="Колонки"> 
     <MenuItem >    
      <MenuItem.Template> 
       <ControlTemplate> 
         <ListBox Name="MyComboBox" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},Path=Tag}"> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <CheckBox IsChecked="{Binding Path=Visibility, Mode=TwoWay, Converter={StaticResource BooleanToVisibilityConverter1}}" Content="{Binding Path=Header}"></CheckBox> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 
        </ControlTemplate> 
      </MenuItem.Template> 
       </MenuItem> 
     </MenuItem> 
     <MenuItem Header="Друк"></MenuItem> 
    </ContextMenu> 
</DataGrid.ContextMenu>