using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace PropertyGridDemo.PropertyGridControl
{
/// <summary>
///初始化所有属性并调用模板选择器进行匹配
/// </summary>
public class DataEditorsViewModel
{
public IEnumerable<PropertyDescriptor> Properties { get { return TypeDescriptor.GetProperties(typeof(TestPropertyGrid)).Cast<PropertyDescriptor>(); } }
}
}
3.编写一个可用于构建模板的属性 CustomUIType:
using System;
namespace PropertyGridDemo.PropertyGridControl
{
public class CustomUIType
{
}
public enum CustomUITypes
{
Slider,
ComboBoxEit,
SpinEdit,
CheckBoxEdit
}
[AttributeUsage(AttributeTargets.Property)]
internal class CustomUIAttribute : Attribute
{
public string DataContextPropertyName { get; set; }
public CustomUITypes CustomUI { get; set; }
/// <summary>
/// 自定义控件属性构造函数
/// </summary>
/// <param name="uiTypes">The UI types.</param>
/// <param name="dataContextPropertyName">Name of the data context property.</param>
internal CustomUIAttribute(CustomUITypes uiTypes, string dataContextPropertyName)
{
CustomUI = uiTypes;
DataContextPropertyName = dataContextPropertyName;
}
}
}
4.编写对应的DataContext类 TestPropertyGrid:
using DevExpress.Mvvm.DataAnnotations;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Timers;
using System.Windows;
namespace PropertyGridDemo.PropertyGridControl
{
[MetadataType(typeof(DynamicallyAssignDataEditorsMetadata))]
public class TestPropertyGrid : PropertyDataContext
{
private double _count = 0;
private SliderUIDataContext _countSource = null;
private ComboBoxEditDataContext _comboSource = null;
private double _value=1;
public TestPropertyGrid()
{
Password = "1111111";
Notes = "Hello";
Text = "Hello hi";
}
[Browsable(false)]
public SliderUIDataContext CountSource
{
get
{
if (_countSource != null)
{
return _countSource;
}
else
{
_countSource = new SliderUIDataContext(0, 100, Count, 0.1, 1);
_countSource.PropertyChanged += (object o, PropertyChangedEventArgs e) =>
{
this.Count = _countSource.Value;
};
return _countSource;
}
}
}
[Browsable(false)]
public ComboBoxEditDataContext ComboSource
{
get
{
if(_comboSource==null)
{
_comboSource =new ComboBoxEditDataContext(ComboBoxEditItemSource.TestItemSource,Value);
_comboSource.PropertyChanged += (object o, PropertyChangedEventArgs e) =>
{
this.Value =Convert.ToDouble(_comboSource.EditValue.Item2);
};
}
return _comboSource;
}
}
[Display(Name = "SliderEdit", GroupName = "CustomUI")]
[CustomUI(CustomUITypes.Slider, nameof(CountSource))]
public double Count
{
get => _count;
set
{
_count = value;
CountSource.Value = value;
RaisePropertyChanged(nameof(Count));
}
}
[Display(Name = "ComboBoxEditItem", GroupName = "CustomUI")]
[CustomUI(CustomUITypes.ComboBoxEit, nameof(ComboSource))]
public double Value
{
get => _value;
set
{
if (_value == value) return;
_value = value;
//ComboSource.Value = value;
RaisePropertyChanged(nameof(Value));
}
}
[Display(Name = "Password", GroupName = "DefaultUI")]
public string Password { get; set; }
[Display(Name = "TextEdit", GroupName = "DefaultUI")]
public string Text { get; set; }
[Display(Name = "Notes", GroupName = "DefaultUI")]
public string Notes { get; set; }
[Display(Name = "Double", GroupName = "DefaultUI")]
[DefaultValue(1)]
public double TestDouble { get; set; }
[Display(Name = "Items", GroupName = "DefaultUI")]
[DefaultValue(Visibility.Visible)]
public Visibility TestItems { get; set; }
}
public static class DynamicallyAssignDataEditorsMetadata
{
public static void BuildMetadata(MetadataBuilder<TestPropertyGrid> builder)
{
builder.Property(x => x.Password)
.PasswordDataType();
builder.Property(x => x.Notes)
.MultilineTextDataType();
}
}
}










