C#覆盖DependencyProperty标记已过时

时间:2018-07-17 07:38:13

标签: c# wpf

我有一个具有DependencyProperty的自定义WPF控件:

    public static readonly DependencyProperty DeferedVisibilityProperty;


    private static void OnDeferedVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var control = (CircularProgressBar)d;
        control.OnDeferedVisibilityChanged();
    }


    public bool DeferedVisibility {
        get => (bool)GetValue(DeferedVisibilityProperty);
        set => SetValue(DeferedVisibilityProperty, value);
    }


    private void OnDeferedVisibilityChanged() {
        if(DeferedVisibility) {
            VisualStateManager.GoToState(this, "Visible", true);
            Visibility = Visibility.Visible;
        } else {
            VisualStateManager.GoToState(this, "Collapsed", true);
            Visibility = Visibility.Collapsed;
        }
    }

现在,我们可以设置可见性而无需bool-to-visibility转换器。 使用Visibility DependencyProperty时,它不再起作用,因此我想将其标记为过时。

我知道您可以像这样[Obsolete("Please use DeferedVisibility")]标记成员。

那么如何将基类的Visibility属性标记为过时?

1 个答案:

答案 0 :(得分:2)

只需重写Visibility依赖项属性:

[Obsolete("Use something else")]
public new Visibility Visibility
{
    get { return (Visibility)GetValue(VisibilityProperty); }
    set { SetValue(VisibilityProperty, value); }
}

[Obsolete("Use something else")]
public new static readonly DependencyProperty VisibilityProperty =
    DependencyProperty.Register(nameof(Visibility), typeof(Visibility), typeof(YourCustomControl), new PropertyMetadata(0));

但是我看到的问题是,过时只会在代码后面进行标记,而不会在XAML中进行标记(至少在我看来如此)