zoukankan      html  css  js  c++  java
  • 在通过数据绑定的菜单中插入Seperator

    WPF中通过可以很容易通过数据绑定生成菜单,例如:

    <Window.Resources>
        <local:Source x:Key="src"/>
    </Window.Resources>
    <StackPanel>
        <Menu>
            <MenuItem Header="Animals" ItemsSource="{Binding Source={StaticResource src}}" />
        </Menu>
    </StackPanel>

    public
    class Source : ObservableCollection<object>

    {

        public Source()

        {

        
        //Spiders
            Add("Golden Silk Spider");

            Add("Black Widow Spider");


        
        //BigCats
            Add("Jaguar"
    );

            Add("African Wildcat");

            Add("Cheetah");


        
        //Amphibians
        
        
    Add
    ("California Newt");

            Add("Tomato Frog");

            Add("Green Tree Frog");

        }

    }

    这段代码生成了如下所示的菜单

    这种方式简单易用,但有个问题,缺乏灵活性:比如说如果我们要根据不同的类别分组,通过seperator隔离,生成如下图所示的菜单,这时该如何做呢?

    我试过几种方法,如设置ItemStyleSelecter,DataTemplate等,效果都不是很好,最后找到一种很简单的方法:在数据源中直接插入Seperator即可

    public class Source : ObservableCollection<object>
    {
        public Source()
        {
            
            Add("Golden Silk Spider");
            Add("Black Widow Spider");

            Add(new Separator());

            Add("Jaguar");
            Add("African Wildcat");
            Add("Cheetah");

            Add(new Separator());

            Add("California Newt");
            Add("Tomato Frog");
            Add("Green Tree Frog");
        }
    }

    这种方式简单易用,并且能保持和系统主题样式一致,是目前我知道的最好的方法了。ToobBar的Seperator也可以用这种方式生成。

  • 相关阅读:
    asynDBCenter(修改)
    asynDBcenter(复习)
    随笔
    mongo二维数组操作
    function复习
    Java源码更改的方式
    Struts has detected an unhandled exception
    创建template模板
    aused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method fai
    node to traverse cannot be null
  • 原文地址:https://www.cnblogs.com/TianFang/p/1517390.html
Copyright © 2011-2022 走看看