依赖属性默认值
我是 WPF 和依赖属性的新手,我的问题可能完全是新手...
我有以下依赖属性:
public static readonly DependencyProperty IsEditableProperty =
DependencyProperty.Register("IsEditable", typeof(bool), typeof(EditContactUserControl),
new FrameworkPropertyMetadata(false, OnIsEditablePropertyChanged));
public bool IsEditable
{
get { return (bool)GetValue(IsEditableProperty); }
set { SetValue(IsEditableProperty, value); }
}
private static void OnIsEditablePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
EditContactUserControl control = source as EditContactUserControl;
bool isEditable = (bool)e.NewValue;
if (isEditable)
control.stackPanelButtons.Visibility = Visibility.Visible;
else
control.stackPanelButtons.Visibility = Visibility.Collapsed;
}
问题是我希望在 OnIsEditablePropertyChanged
中执行代码我的属性的默认值,这不会发生。
我做错了什么,或者按照您的意见我应该怎么做?
先感谢您。
I am new with WPF and dependency properties and my question might be totally newbie...
I have the following dependency property:
public static readonly DependencyProperty IsEditableProperty =
DependencyProperty.Register("IsEditable", typeof(bool), typeof(EditContactUserControl),
new FrameworkPropertyMetadata(false, OnIsEditablePropertyChanged));
public bool IsEditable
{
get { return (bool)GetValue(IsEditableProperty); }
set { SetValue(IsEditableProperty, value); }
}
private static void OnIsEditablePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
EditContactUserControl control = source as EditContactUserControl;
bool isEditable = (bool)e.NewValue;
if (isEditable)
control.stackPanelButtons.Visibility = Visibility.Visible;
else
control.stackPanelButtons.Visibility = Visibility.Collapsed;
}
The problem is that I want to have the code in the OnIsEditablePropertyChanged
to be executed also for the default value of my property, which doesn't happen.
What am I doing wrong, or how should I do this in your opiniion?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在 XAML 中绑定可见性属性并使用布尔值到可见性转换器,而不是更改代码中的可见性。
如果这样做,属性是否初始化并不重要。
Instead of changing the visibility in code, you should Bind the Visibility property in XAML and use a boolean to Visibility Converter.
If you do this, it doesn't matter if the property is initialized or not.
OnPropertyChanged 回调不会在启动时被调用:“默认”值实际上从未真正“设置”。默认值:未设置时的属性值。
如果要在控件启动时执行某些代码,请将其放在 ApplyTemplate 方法重写中(对于 TemplatedControl)或构造函数末尾(对于 UserControl)
避免在构造函数和中重复此代码属性更改回调:将其放入双方调用的通用方法中,即:
The OnPropertyChanged callback won't be called on startup: The "default" value is in fact never really "set". Default: The value of the property when it isn't set.
If you want to execute some code at control startup, put it in the ApplyTemplate method override (in the case of a TemplatedControl) or at the end of your constructor (in the case of a UserControl)
Avoid duplicating this code in the constructor and in the property changed callback: Put it in a common method called by both ie:
我认为更好的方法是在 XAML 中将
stackPanelButtons.Visibility = Visibility.Collapsed
设置为默认值,在这种情况下,您不需要在启动时运行所有这些代码!I think a much better approach would be to set up
stackPanelButtons.Visibility = Visibility.Collapsed
in your XAML as default too, in which case you don't need to run all this code on startup!