C# 更改Silverlight中DependencyProperty的默认值
我创建了一个从另一个控件继承的UserControl。我现在搜索一种方法,以更改BaseControl的DependencyProperty的defaultvalue。在WPF中,我可以使用OverrideMetadata覆盖DependecProperty的默认值。但Silverlight中不提供OverrideMetadata 有人能给我一个提示,我如何在Silverlight中更改DependencyProperty的DefaultValueC# 更改Silverlight中DependencyProperty的默认值,c#,silverlight,silverlight-5.0,C#,Silverlight,Silverlight 5.0,我创建了一个从另一个控件继承的UserControl。我现在搜索一种方法,以更改BaseControl的DependencyProperty的defaultvalue。在WPF中,我可以使用OverrideMetadata覆盖DependecProperty的默认值。但Silverlight中不提供OverrideMetadata 有人能给我一个提示,我如何在Silverlight中更改DependencyProperty的DefaultValue public static r
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MyClass), new PropertyMetadata(string.Empty));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
在此部件上,您正在设置默认值:
new PropertyMetadata(string.Empty)
它将始终使用空字符串启动
编辑:
我真的很想解决这个问题。我认为在一个解决方案中,似乎不是最正确的一个,我们可以一起努力改进。
在基类中执行此操作:
public virtual void DefineDefaultValue(object default)
{
defaultValue = default;
OnPropertyChanged("MyProperty");
}
static object defaultValue;
public static object Define()
{
return defaultValue;
}
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(MainMenuBase), new PropertyMetadata((int)Define()));
毕竟,在类构造函数中执行以下操作:
public void ClassConstructor()
{
DefineDefaultValue("BlaBlaBla");
}
public override void DefineDefaultValue(object default)
{
base.DefineDefaultValue(default);
}
谢谢你的回答。但在我的例子中,我的控件继承自另一个控件,例如,该控件已经具有DefaultValue string.Empty的TextProperty。我现在只想在我的控件中更改DefaultValue,例如更改为blabla。不要创建新属性!哦,对不起。我一直在寻找答案,但没有找到。只设置本地值对您没有帮助,是吗?嗯,好主意,但我无法更改DependencyProperty“MyProperty”,因为我无法访问从中继承的基类。因此,我无法更改用于获取PropertyMetaData中的DefaultValue的Logik。那么在基类中实现singleton呢?它是类的一种静态实例。这样,您可以通过调用MyClass.Instance来访问它的属性。我无权更改基类。