DevExpress框架PropertyGridControl自定义属性,弹出框随意定制逻辑,自动设置属性到Model实体类

发布时间 2023-09-05 22:55:32作者: Yann_123

DevExpress 框架 WPF
PropertyGridControl 组件

自定义属性弹出框,支持同类型属性多次设置,一次编写多次复用

XAML 代码
    <dxprg:PropertyGridControl Name="property">
        <dxprg:PropertyDefinition Type="models:CustModel">
            <dxprg:PropertyDefinition.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition Width="20"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Label Name="ShowTextLabel" Tag="{Binding Value}" Content="{Binding Value.Name}" Grid.Column="0"></Label>
                        <Button Grid.Column="1" Content="···" Tag="{Binding ElementName=ShowTextLabel}" Foreground="Gray" BorderBrush="Gray" Padding="0" Click="ButtonEditSettings_DefaultButtonClick"></Button>
                    </Grid>
                </DataTemplate>
            </dxprg:PropertyDefinition.CellTemplate>
        </dxprg:PropertyDefinition>
    </dxprg:PropertyGridControl>
    WindowDialog dialog = new WindowDialog();
    if ((bool)dialog.ShowDialog())
    {
        Button button = sender as Button;
        Label label = button.Tag as Label;
        CustModel model = label.Tag as CustModel;
        //反射获取属性
        PropertyInfo[] propertyInfos = property.SelectedObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
        PropertyInfo modelInfo = null;

        foreach (var item in propertyInfos)
        {
            if (item.GetValue(property.SelectedObject)== model)
            {
                modelInfo = item;
            }
        }
        if (model != null && model != null && label != null&& modelInfo!=null)
        {
            model.Name = dialog.GetText();
            label.Content = model.Name;
            modelInfo.SetValue(property.SelectedObject, model);
        }
    }
    public class CustModel
    {
        private string name = "Name";
        public CustModel()
        {

        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }
    public class Person
    {
        CustModel custModel1 = new CustModel();
        public CustModel CModel
        {
            get
            {
                return custModel1;
            }
            set
            {
                custModel1 = value;
            }
        }
        public CustModel CModel1 { get; set; } = new CustModel();
        public CustModel CModel2 { get; set; } = new CustModel();
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public DateTime Time { get; set; } = DateTime.Now;

    }
运行效果

属性界面
编辑界面

源码地址

码云仓库地址

技术交流

  • 感兴趣的可以加 QQ 群:765907694 一起交流,探讨技术问题