HandyControl PropertyGrid 属性

3 篇文章 0 订阅

可自动为模型对象中的属性生成与之相匹配的编辑器集合.

1
2
3
[TemplatePart(Name = ElementItemsControl, Type = typeof(ItemsControl))]
[TemplatePart(Name = ElementSearchBar, Type = typeof(SearchBar))]
public class PropertyGrid : Control

属性

属性描述默认值备注
PropertyResolver属性解析器new PropertyResolver
SelectedObject模型对象
Description描述
MaxTitleWidth最大标题宽度0
MinTitleWidth最小标题宽度0

自带编辑器

名称说明
DatePropertyEditor日期编辑器
DateTimePropertyEditor日期时间编辑器
EnumPropertyEditor枚举编辑器
HorizontalAlignmentPropertyEditor水平对齐方式编辑器
ImagePropertyEditor图片编辑器
NumberPropertyEditor数字编辑器
PlainTextPropertyEditor纯文本编辑器
ReadOnlyTextPropertyEditor只读文本编辑器
SwitchPropertyEditor布尔编辑器(开关风格)
TimePropertyEditor时间编辑器
VerticalAlignmentPropertyEditor垂直对齐方式编辑器

事件

名称说明
SelectedObjectChanged当模型对象改变时触发

案例

基础用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class PropertyGridDemoModel
{
    [Category("Category1")]
    public string String { get; set; }

    [Category("Category2")]
    public int Integer { get; set; }

    [Category("Category2")]
    public bool Boolean { get; set; }

    [Category("Category1")]
    public Gender Enum { get; set; }

    public HorizontalAlignment HorizontalAlignment { get; set; }

    public VerticalAlignment VerticalAlignment { get; set; }

    public ImageSource ImageSource { get; set; }
}

public enum Gender
{
    Male,
    Female
}
1
2
3
4
5
6
7
8
DemoModel = new PropertyGridDemoModel
{
    String = "TestString",
    Enum = Gender.Female,
    Boolean = true,
    Integer = 98,
    VerticalAlignment = VerticalAlignment.Stretch
};
1
<hc:PropertyGrid Width="500" SelectedObject="{Binding DemoModel}"/>

自定义编辑器

我们以 PlainTextPropertyEditor 为例,当需要自定义编辑器,我们可以从 PropertyEditorBase 继承,并重写某些方法,这些方法的定义如下表所示:

名称说明备注
CreateElement创建具体操作控件必须重写
CreateBinding为具体操作控件创建数据绑定
GetDependencyProperty获取具体操作控件中需要绑定的依赖属性必须重写
GetBindingMode获取绑定模式
GetUpdateSourceTrigger获取更新数据源的触发模式
GetConverter获取绑定时需要使用的转换器

纯文本编辑器的具体操作控件可以是 TextBox

1
2
3
4
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
{
    IsReadOnly = propertyItem.IsReadOnly
};

需要绑定的依赖属性应该是 TextProperty

1
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;

最后整体的代码如下:

1
2
3
4
5
6
7
8
9
public class PlainTextPropertyEditor : PropertyEditorBase
{
    public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
    {
        IsReadOnly = propertyItem.IsReadOnly
    };

    public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值