none
Context Menu 绑定的问题 RRS feed

  • 问题

  • <Button Content="Button" Height="26" HorizontalAlignment="Left" VerticalAlignment="Top" Width="94" Margin="0,94,0,0">
                <Button.ContextMenu>
                    <ContextMenu MenuItem.Click="aaaa" ItemsSource="{Binding listDataMenu}" ItemContainerStyle="{StaticResource ContextMenuItemStyle}"/>
                </Button.ContextMenu>
     </Button>

    求助,怎么可以通过绑定的方式,绑定一个<Separator/>分隔线上去


    不吝赐教
    2011年9月8日 13:31

答案

  • XAML:

      <Grid>
        <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Test">
          <Button.ContextMenu>
            <ContextMenu ItemsSource="{Binding}">
              <ContextMenu.ItemContainerStyle>
                <Style TargetType="MenuItem">
                  <Setter Property="Header" Value="{Binding Title}"/>
                  <Style.Triggers>
                    <DataTrigger Binding="{Binding Category}" Value="2">
                      <Setter Property="Template" >
                        <Setter.Value>
                          <ControlTemplate>
                            <Separator Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}"/>
                          </ControlTemplate>
                        </Setter.Value>
                      </Setter>
                    </DataTrigger>
                  </Style.Triggers>
                </Style>
              </ContextMenu.ItemContainerStyle>
            </ContextMenu>
          </Button.ContextMenu>
        </Button>
      </Grid>
    

    C#:

      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          this.DataContext = new List<Item>()
          {
            new Item(){Title="Menu Item 1", Category=MenuCategory.TEXT},
            new Item(){Title="Menu Item 2", Category=MenuCategory.TEXT},
            new Item(){Title="Menu Item 3", Category=MenuCategory.TEXT},
            new Item(){Title="", Category=MenuCategory.SEPARATOR},
            new Item(){Title="Menu Item 4", Category=MenuCategory.TEXT},
            new Item(){Title="Menu Item 5", Category=MenuCategory.TEXT},
          };
        }
      }
    
      public enum MenuCategory
      {
        TEXT = 1,
        SEPARATOR = 2
      }
    
      public class Item
      {
        public MenuCategory Category { get; set; }
        public string Title { get; set; }
      }
    

    看我上面的例子。


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 kyle.cj 2011年9月12日 2:53
    2011年9月9日 6:02
    版主

全部回复

  • 分割线?楼主是想要分割的效果吧,重写Itemtemplate试试吧

    2011年9月8日 13:57
  • XAML:

      <Grid>
        <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Test">
          <Button.ContextMenu>
            <ContextMenu ItemsSource="{Binding}">
              <ContextMenu.ItemContainerStyle>
                <Style TargetType="MenuItem">
                  <Setter Property="Header" Value="{Binding Title}"/>
                  <Style.Triggers>
                    <DataTrigger Binding="{Binding Category}" Value="2">
                      <Setter Property="Template" >
                        <Setter.Value>
                          <ControlTemplate>
                            <Separator Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}"/>
                          </ControlTemplate>
                        </Setter.Value>
                      </Setter>
                    </DataTrigger>
                  </Style.Triggers>
                </Style>
              </ContextMenu.ItemContainerStyle>
            </ContextMenu>
          </Button.ContextMenu>
        </Button>
      </Grid>
    

    C#:

      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          this.DataContext = new List<Item>()
          {
            new Item(){Title="Menu Item 1", Category=MenuCategory.TEXT},
            new Item(){Title="Menu Item 2", Category=MenuCategory.TEXT},
            new Item(){Title="Menu Item 3", Category=MenuCategory.TEXT},
            new Item(){Title="", Category=MenuCategory.SEPARATOR},
            new Item(){Title="Menu Item 4", Category=MenuCategory.TEXT},
            new Item(){Title="Menu Item 5", Category=MenuCategory.TEXT},
          };
        }
      }
    
      public enum MenuCategory
      {
        TEXT = 1,
        SEPARATOR = 2
      }
    
      public class Item
      {
        public MenuCategory Category { get; set; }
        public string Title { get; set; }
      }
    

    看我上面的例子。


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 kyle.cj 2011年9月12日 2:53
    2011年9月9日 6:02
    版主
  • XAML:

    <Grid> ... ... </Grid>


    C#: 

      public partial class MainWindow : Window

      {

       ... ...
      }


    看我上面的例子。

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    太好了,多谢,,, 我试过,,可以的


    不吝赐教
    • 已标记为答案 kyle.cj 2011年9月12日 2:52
    • 取消答案标记 kyle.cj 2011年9月12日 2:53
    2011年9月12日 2:52