在 C1PropertyGrid 中使用自定义编辑控件

C1 控件套包中为 WPF 和 Silverlight 平台提供了 C1PropertyGrid 控件,可以通过该控件来设置控件属性,本文主要演示如何在 C1PropertyGrid for Silverlight 中使用自定义的属性编辑控件,我们将使用的是基于 C1ComboBox 的自定义编辑器控件。

发布于 2013/06/20 00:00

C1 控件套包中为 WPF 和 Silverlight 平台提供了 C1PropertyGrid 控件,可以通过该控件来设置控件属性,本文主要演示如何在 C1PropertyGrid for Silverlight 中使用自定义的属性编辑控件,我们将使用的是基于 C1ComboBox 的自定义编辑器控件。

核心代码是创建一个继承于 C1ComboBox ,并实现 ITypeEditorControl 接口的控件,代码如下:

    public class ComboBoxEditor : C1ComboBox, ITypeEditorControl
    {
        public void Attach(PropertyAttribute property)
        {
            // 设置自定义控件与属性的绑定关系
            var binding = new System.Windows.Data.Binding(property.PropertyInfo.Name)
            {
                Mode = System.Windows.Data.BindingMode.TwoWay,
                Source = property.SelectedObject,
                ValidatesOnExceptions = true,
            };
            this.SetBinding(ComboBoxEditor.SelectedItemProperty, binding);
        }
        public ITypeEditorControl Create()
        {
            ComboBoxEditor cbe = new ComboBoxEditor();
            // 绑定 C1ComboBox 的 ItemsSource 属性
            LoadItems(cbe);
            return cbe;
        }
        
        private void LoadItems(ComboBoxEditor cbe)
        {
            List<KeyValuePair<string, string>> items = new List<KeyValuePair<string, string>>();
            items.Add(new KeyValuePair<string, string>("XA", "西安"));
            items.Add(new KeyValuePair<string, string>("BJ", "北京"));
            items.Add(new KeyValuePair<string, string>("TJ", "天津"));
            items.Add(new KeyValuePair<string, string>("SH", "上海"));
            items.Add(new KeyValuePair<string, string>("CQ", "重庆"));
            cbe.ItemsSource = items;
            cbe.DisplayMemberPath = "Value";
            cbe.SelectedValuePath = "Key";
        }
        public void Detach(PropertyAttribute property)
        {
            
        }
        public bool Supports(PropertyAttribute Property)
        {
            return Property.PropertyInfo.PropertyType == typeof(System.String);
        }
        public event System.ComponentModel.PropertyChangedEventHandler ValueChanged;
    }

 

在页面后台代码中使用自定义的 ComboBoxEditor 控件,代码如下:

    public MainPage()
    {
        InitializeComponent();
            Item item = new Item();
          
        c1PropertyGrid1.AvailableEditors.Insert(0, new ComboBoxEditor());            
        c1PropertyGrid1.SelectedObject = item;
        c1PropertyGrid1.PropertyAttributes.Add(new PropertyAttribute()
        {
            MemberName = "City",
            Editor = new ComboBoxEditor()
        });
    }

 

运行截图:

C1PropertyGrid

 

源码下载:

关于葡萄城

葡萄城是专业的软件开发技术和低代码平台提供商,以“赋能开发者”为使命,致力于通过表格控件、低代码和BI等各类软件开发工具和服务,一站式满足开发者需求,帮助企业提升开发效率并创新开发模式。葡萄城开发技术始于1980年,40余年来始终聚焦软件开发技术,有深厚的技术积累和丰富的产品线。是业界能够同时赋能软件开发和低代码开发的企业。凭借过硬的产品能力、活跃的用户社区和丰富的伙伴生态,与超过3000家合作伙伴紧密合作,产品广泛应用于信息和软件服务、制造、交通运输、建筑、金融、能源、教育、公共管理等支柱产业。

推荐相关案例
推荐相关资源
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态