wpf devexpress Property Grid创建属性定义

WPF Property Grid控件使用属性定义定义如何做和显示

本教程示范如何绑定WP Property Grid控件到数据和创建属性定义。

执行如下步骤

第一步-创建属性定义

添加PropertyGridControl组件到项目。

打开工具箱在vs,定位到DX.23.1: Data 面板,选择PropertyGridControl工具箱选项,拖动到窗口。

右键点击Property Grid选择Layout | Reset All填充全部窗口:

第二步-创建数据对象

创建数据对象和设置到DataContext

namespace Creating_Definitions {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            DataContext = new Customer() {
                ID = 1,
                FirstName = "Nancy",
                LastName = "Davolio",
                Gender = Gender.Female,
                BirthDate = new DateTime(1948, 8, 12),
                Phone = "7138638137"
            };
  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF中使用Xceed PropertyGrid控件显示枚举的中文值需要进行以下步骤: 1. 在ViewModel或Model类中定义一个枚举类型的属性,例如: ``` public enum Gender { Male, Female } private Gender _selectedGender; public Gender SelectedGender { get { return _selectedGender; } set { _selectedGender = value; } } ``` 2. 在XAML中引入Xceed PropertyGrid的命名空间,并添加PropertyGrid控件,例如: ```xml xmlns:xceed="clr-namespace:Xceed.Wpf.Toolkit.PropertyGrid;assembly=Xceed.Wpf.Toolkit" <xceed:PropertyGrid Name="propertyGrid" /> ``` 3. 在后台代码中,将枚举类型的属性绑定到PropertyGrid控件的SelectedObject属性,并使用Xceed PropertyGrid的下拉框编辑器进行自定义显示,例如: ```csharp var enumEditor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.EnumEditor(); enumEditor.EnumComboBoxStyle = new Style(typeof(ComboBox)); enumEditor.EnumComboBoxStyle.Setters.Add(new Setter(ComboBox.ItemTemplateProperty, new DataTemplate() { VisualTree = new FrameworkElementFactory(typeof(TextBlock)) })); propertyGrid.SelectedObject = this; propertyGrid.PropertyDefinitions.Add(new PropertyDefinition() { Name = "SelectedGender", DisplayName = "性别", Editor = enumEditor }); ``` 4. 在Resource文件中定义一个XAML资源字典,用于显示枚举的中文值,例如: ```xml <ResourceDictionary> <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="GenderValues"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:Gender" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </ResourceDictionary> ``` 5. 最后,在ComboBox的ItemTemplate中绑定枚举值对应的中文字符串,使用上述定义的资源字典,例如: ```xml enumEditor.EnumComboBoxStyle.Setters.Add(new Setter(ComboBox.ItemTemplateProperty, new DataTemplate() { VisualTree = new FrameworkElementFactory(typeof(TextBlock)), DataTemplate.VisualTree = new DataTemplate(typeof(TextBlock)), DataTemplate.VisualTree.Setters.Add(new Setter(TextBlock.TextProperty, new Binding(".") { Mode = BindingMode.OneWayToSource, Source = { StaticResource GenderValues } } ) ) } )); ``` 这些步骤会使Xceed PropertyGrid控件能够正确地显示枚举的中文值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赫敏璋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值