wpf devexpress Property Grid创建属性定义

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

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

执行如下步骤

第一步-创建属性定义

添加PropertyGridControl组件到项目。

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

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

第二步-创建数据对象

创建数据对象和设置到DataContext

cs 复制代码
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"
            };
        }
        public class Customer {
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public Gender Gender { get; set; }
            public DateTime BirthDate { get; set; }
            public string Phone { get; set; }
        }
        public enum Gender { Male, Female }
    }
}

第三步-绑定Property Grid到Data Object

使用property grid PropertyGridControl.SelectedObject 属性绑定数据

XML 复制代码
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid" x:Class="PG_lesson1.MainWindow"
        Title="MainWindow" Height="250" Width="525">
    <Grid>

        <dxprg:PropertyGridControl SelectedObject="{Binding}" />

    </Grid>
</Window>

步骤四-创建属性定义

添加属性定义到Property Grid.设置PropertyGridControl.ShowProperties属性ShowPropertiesMode.WithPropertyDefinitions,隐藏未定义属性:

XML 复制代码
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid" x:Class="PG_lesson1.MainWindow"
        Title="MainWindow" Height="250" Width="525">
    <Grid>

        <dxprg:PropertyGridControl SelectedObject="{Binding}" ShowProperties="WithPropertyDefinitions" >
            <dxprg:PropertyDefinition Type="sys:String" />
            <dxprg:PropertyDefinition Path="Gender" />
            <dxprg:PropertyDefinition Path="BirthDate" />
        </dxprg:PropertyGridControl>

    </Grid>
</Window>

运行程序看到结果

相关推荐
xcLeigh1 天前
WPF进阶 | WPF 数据绑定进阶:绑定模式、转换器与验证
c#·wpf
学与用3 天前
【deepseek实战】绿色好用,不断网
ai·c#·wpf
的不对不4 天前
WPF基础03——InitializeComponent()函数解释
windows·c#·.net·wpf
军训猫猫头4 天前
61.异步编程1 C#例子 WPF例子
开发语言·c#·wpf
时光追逐者4 天前
一组开源、免费、Metro风格的 WPF UI 控件库
ui·开源·c#·.net·wpf·.netcore·微软技术
军训猫猫头5 天前
58.界面参数传递给Command C#例子 WPF例子
开发语言·ui·c#·wpf
xcLeigh6 天前
WPF基础 | 深入 WPF 事件机制:路由事件与自定义事件处理
c#·wpf
军训猫猫头7 天前
60.await与sleep的原理分析 C#例子 WPF例子
开发语言·ui·c#·wpf
敲代码的TKP8 天前
WPF自定义布局--瀑布布局
wpf
xcLeigh8 天前
WPF基础 | WPF 基础概念全解析:布局、控件与事件
c#·wpf