C# 在属性中传递依赖属性
C# passing dependency properties in an attribute
我正在 WPF 中创建一个自编曲 UserControl
。
我有一个属性来装饰我的实体的属性,它指示我 SelfComposingUserControl
做什么。
如果我希望 属性 由特定的 UI 控件编辑,我将其传递到我的属性中。
在我的属性中,我真的不确定如何在 UI 控件上传递哪个 属性 我想绑定到我的实体 属性。
有人可以帮忙吗?
这是我的 UIEditableAttribute
:
的精简版
public class UIEditableAttribute : Attribute
{
/// <summary>
/// UIControl to edit the property
/// </summary>
public Type UIControl { get; set; }
/// <summary>
/// UIControl dependency property that binds to the property
/// </summary>
public DependencyProperty UIControlValueProperty { get; set; }
}
这里是一个 属性 使用属性
的例子
private int _numberOfRows;
[UIEditable(DisplayGroup = "B", UIControl = typeof(RadNumericUpDown), UIControlValueProperty = RadNumericUpDown.ValueProperty)]
public int NumberOfRows
{
get { return _numberOfRows; }
set { CheckPropertyChanged(ref _numberOfRows, value); }
}
在 SelfComposingUserControl
中,我通过对 UIControl
类型使用 switch 语句来解决这个问题,该语句将 属性 绑定到正确的依赖项 属性。但是,我想在 属性 级别指定。
非常感谢。
答案是将 UIControlValueProperty 作为字符串传递,即在属性中 "ValueProperty",然后得到这个有用的函数;
Get Dependency Property By Name
从类型或对象中获取依赖项属性。
我正在 WPF 中创建一个自编曲 UserControl
。
我有一个属性来装饰我的实体的属性,它指示我 SelfComposingUserControl
做什么。
如果我希望 属性 由特定的 UI 控件编辑,我将其传递到我的属性中。
在我的属性中,我真的不确定如何在 UI 控件上传递哪个 属性 我想绑定到我的实体 属性。
有人可以帮忙吗?
这是我的 UIEditableAttribute
:
public class UIEditableAttribute : Attribute
{
/// <summary>
/// UIControl to edit the property
/// </summary>
public Type UIControl { get; set; }
/// <summary>
/// UIControl dependency property that binds to the property
/// </summary>
public DependencyProperty UIControlValueProperty { get; set; }
}
这里是一个 属性 使用属性
的例子 private int _numberOfRows;
[UIEditable(DisplayGroup = "B", UIControl = typeof(RadNumericUpDown), UIControlValueProperty = RadNumericUpDown.ValueProperty)]
public int NumberOfRows
{
get { return _numberOfRows; }
set { CheckPropertyChanged(ref _numberOfRows, value); }
}
在 SelfComposingUserControl
中,我通过对 UIControl
类型使用 switch 语句来解决这个问题,该语句将 属性 绑定到正确的依赖项 属性。但是,我想在 属性 级别指定。
非常感谢。
答案是将 UIControlValueProperty 作为字符串传递,即在属性中 "ValueProperty",然后得到这个有用的函数;
Get Dependency Property By Name
从类型或对象中获取依赖项属性。