在 null 时设置属性的默认值
Set default value on properties when null
我正在尝试定义一种在 C# 中为我的属性设置默认值的方法。
我有很多元素 TextBox 和一个恢复默认值的按钮。我希望当用户单击按钮时,TextBox 的内容设置为默认值。
我的想法是将值设置为 null,然后在 setter 中检查值,如果值为 null,则设置默认值。
这是我的代码:
private void SetDefault()
{
Grid actualItem = tabControl.SelectedContent as Grid;
if (actualItem != null)
{
foreach (object elt in actualItem.Children)
{
if (elt.GetType() == typeof(TextBox))
{
TextBox box = elt as TextBox;
box.Text = null;
}
}
}
}
文本框示例:
<TextBox Grid.Row="1" Grid.Column="1"
Style="{StaticResource TextBoxStyle}"
Text="{Binding ExamplePropertie,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
对于每个绑定元素,我想做这样的事情:
[DefaultValue("default")]
public String ExamplePropertie
{
get
{
return _myProp;
}
set
{
if (value == null)
default(ExamplePropertie); // Not working
else
_myProp = value;
OnPropertyChanged(nameof(ExamplePropertie));
}
}
但是 default(ExamplePropertie)
不是这样做的好方法。如何设置用 [DefaultValue(value)]
定义的值?
嗯,您可以求助于反射来获取该属性的值,但更简单的方法是将默认值简单地存储为常量:
private const string ExamplePropertieDefault = "default";
[DefaultValue(ExamplePropertieDefault)]
public String ExamplePropertie
{
get
{
return _myProp;
}
set
{
if (value == null)
ExamplePropertieDefault;
else
_myProp = value;
OnPropertyChanged(nameof(ExamplePropertie));
}
}
[DefaultValue(...)]
是一个属性。您无法通过将 default(...)
应用于 属性 的名称来获取其值。这个过程更复杂:您需要为 属性 获取 PropertyInfo
对象,查询它以获得类型 DefaultValueAttribute
的自定义属性,然后才从中获取值。
使用辅助方法,该过程变得更加容易:
static T GetDefaultValue<T>(object obj, string propertyName) {
if (obj == null) return default(T);
var prop = obj.GetType().GetProperty(propertyName);
if (prop == null) return default(T);
var attr = prop.GetCustomAttributes(typeof(DefaultValueAttribute), true);
if (attr.Length != 1) return default(T);
return (T)((DefaultValueAttribute)attr[0]).Value;
}
将此辅助方法单独class,使用如下:
_myProp = value ?? Helper.GetDefaultValue<string>(this, nameof(ExampleProperty));
注意: foreach
循环 SetDefault
可以这样简化:
foreach (object box in actualItem.Children.OfType<TextBox>()) {
box.Text = null;
}
我正在尝试定义一种在 C# 中为我的属性设置默认值的方法。
我有很多元素 TextBox 和一个恢复默认值的按钮。我希望当用户单击按钮时,TextBox 的内容设置为默认值。
我的想法是将值设置为 null,然后在 setter 中检查值,如果值为 null,则设置默认值。
这是我的代码:
private void SetDefault()
{
Grid actualItem = tabControl.SelectedContent as Grid;
if (actualItem != null)
{
foreach (object elt in actualItem.Children)
{
if (elt.GetType() == typeof(TextBox))
{
TextBox box = elt as TextBox;
box.Text = null;
}
}
}
}
文本框示例:
<TextBox Grid.Row="1" Grid.Column="1"
Style="{StaticResource TextBoxStyle}"
Text="{Binding ExamplePropertie,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
对于每个绑定元素,我想做这样的事情:
[DefaultValue("default")]
public String ExamplePropertie
{
get
{
return _myProp;
}
set
{
if (value == null)
default(ExamplePropertie); // Not working
else
_myProp = value;
OnPropertyChanged(nameof(ExamplePropertie));
}
}
但是 default(ExamplePropertie)
不是这样做的好方法。如何设置用 [DefaultValue(value)]
定义的值?
嗯,您可以求助于反射来获取该属性的值,但更简单的方法是将默认值简单地存储为常量:
private const string ExamplePropertieDefault = "default";
[DefaultValue(ExamplePropertieDefault)]
public String ExamplePropertie
{
get
{
return _myProp;
}
set
{
if (value == null)
ExamplePropertieDefault;
else
_myProp = value;
OnPropertyChanged(nameof(ExamplePropertie));
}
}
[DefaultValue(...)]
是一个属性。您无法通过将 default(...)
应用于 属性 的名称来获取其值。这个过程更复杂:您需要为 属性 获取 PropertyInfo
对象,查询它以获得类型 DefaultValueAttribute
的自定义属性,然后才从中获取值。
使用辅助方法,该过程变得更加容易:
static T GetDefaultValue<T>(object obj, string propertyName) {
if (obj == null) return default(T);
var prop = obj.GetType().GetProperty(propertyName);
if (prop == null) return default(T);
var attr = prop.GetCustomAttributes(typeof(DefaultValueAttribute), true);
if (attr.Length != 1) return default(T);
return (T)((DefaultValueAttribute)attr[0]).Value;
}
将此辅助方法单独class,使用如下:
_myProp = value ?? Helper.GetDefaultValue<string>(this, nameof(ExampleProperty));
注意: foreach
循环 SetDefault
可以这样简化:
foreach (object box in actualItem.Children.OfType<TextBox>()) {
box.Text = null;
}