WPF 中的自定义设计时可见性属性

时间:2021-05-27 04:56:46

标签: c# wpf xaml dependency-properties dependencyobject

我有一个复杂的窗口,其中包含基于布尔值可见或折叠的各种控件。我想添加一个自定义属性以在设计时显示所有这些控件。 我对该属性的实现如下所示:

public static class CustomAttributes
{
    private static bool? _inDesignMode;

    public static readonly DependencyProperty Visibility = DependencyProperty.RegisterAttached(
        "Visibility",
        typeof(Visibility),
        typeof(CustomAttributes),
        new PropertyMetadata(VisibilityChanged));

    private static bool InDesignMode
    {
        get
        {
            if (!_inDesignMode.HasValue)
            {
                var prop = DesignerProperties.IsInDesignModeProperty;
                _inDesignMode =
                  (bool)DependencyPropertyDescriptor.FromProperty(prop, typeof(FrameworkElement)).Metadata.DefaultValue;
            }

            return _inDesignMode.Value;
        }
    }

    public static Visibility GetVisibility(DependencyObject dependencyObject)
    {
        return (Visibility)dependencyObject.GetValue(Visibility);
    }

    public static void SetVisibility(DependencyObject dependencyObject, Visibility value)
    {
        dependencyObject.SetValue(Visibility, value);
    }

    private static void VisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (!InDesignMode)
            return;

        d.SetValue(Control.VisibilityProperty, e.NewValue);
    }
}

在 XAML 中,我是这样使用的:

<Button Visibility="{Binding SomeBoolValue, Converter={StaticResource BoolToVisibility}}"
        helper:CustomAttributes.Visibility="Visible" 
/>

然而,它似乎不起作用。我使用了其他一些像这样的自定义属性,它们可以完成它们的工作,但是可见性不会触发,它只是在设计视图中保持折叠状态。我错过了什么?

编辑:

感谢您为我指明了正确的方向。我的问题的解决方案不需要自定义属性,就像我最初认为的那样。为了实现我想要的设计时行为,我按照下面接受的答案中的建议修改了转换器实现。

1 个答案:

答案 0 :(得分:1)

深入思考您创建的逻辑。

UI 元素没有两个 Visibility 属性,它是唯一的一个。
但是你想同时通过两种方式来操作这个属性:通过绑定和附加属性。
因此,你在他们之间为这个财产创造了竞争。
并且该属性将采用最后分配给它的值。

附加属性只会在 Button 初始化时触发一次(来自示例)。
当数据上下文和/或其 SomeBoolValue 属性更改时,将触发绑定。
但是Window的Data Context设置晚于这个Window的UI元素的初始化。

我看到了几种解决方案。
如果您需要始终在设计模式中显示元素,最简单的方法是向转换器添加适当的逻辑。
以最简单的形式,此类转换器的示例:

/// <summary>Bool to Visibility converter.</summary>
[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToVisibilityConverter : IValueConverter
{
    public static bool IsDesignMode { get; } = DesignerProperties.GetIsInDesignMode(new DependencyObject());

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool val)
            return IsDesignMode || val 
                ? Visibility.Visible
                : Visibility.Collapsed;

        return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
相关问题