依赖属性默认值

发布于 2024-11-02 14:03:30 字数 1026 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2024-11-09 14:03:30

您应该在 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.

老旧海报 2024-11-09 14:03:30

OnPropertyChanged 回调不会在启动时被调用:“默认”值实际上从未真正“设置”。默认值:未设置时的属性值。

如果要在控件启动时执行某些代码,请将其放在 ApplyTemplate 方法重写中(对于 TemplatedControl)或构造函数末尾(对于 UserControl)

避免在构造函数和中重复此代码属性更改回调:将其放入双方调用的通用方法中,即:

void OnIsEditableChangedImpl(bool newValue)
{
   ....
}

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:

void OnIsEditableChangedImpl(bool newValue)
{
   ....
}
梦亿 2024-11-09 14:03:30

我认为更好的方法是在 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文