隐藏Visual Studio设计器中的WPF元素

Hei*_*nzi 25 wpf visual-studio-2008

我有一个WPF表单,基本上看起来像这样:

<Window ...>
  <Grid>
    <DockPanel>
      [content shown during normal operation]
    </DockPanel>

    <Grid Background="#CCCC" Visibility="Hidden">
      [overlay grid which is only shown during special circumstances]
    </Grid>
  </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

覆盖网格隐藏其他所有内容(即"正常内容"),仅在特殊情况下显示(即网络连接断开).运行程序时,这非常正常.

现在,在设计模式下,问题是Visual Studio忽略了Visibility="Hidden".通常,这是完全合理的(毕竟,我希望能够编辑隐藏的UI元素),但在我的情况下,它很烦人,因为它阻止我编辑设计器中DockPanel中的东西.

所以,我想做的是这样的:

<Grid Background="#CCCC" Visibility="Hidden" VS.ShowInDesigner="False">
  [overlay grid which is only shown during special circumstances]
</Grid>
Run Code Online (Sandbox Code Playgroud)

但是,唉,没有这样的财产,或者至少没有我所知道的财产.有任何想法吗?

ado*_*ace 67

从VS2012开始,您只需使用Blend命名空间IsHidden属性:

  • 添加如果尚未存在xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
  • 将d:IsHidden ="true"放在您想要在设计时隐藏的元素上

  • 不与Visibility属性一起使用.可见性似乎优先考虑.我想没有d:可见性:( (2认同)
  • 凉爽的!在 VS2017 中,混合 xmlns 已经存在,但此解决方案仍然无效(“混合中不存在属性 'IsHidden'...”)。我必须搜索另一个信息并发现,在包含 **xmlns:d="http://schemas.microsoft.com/expression/blend/2008"** 的行之后,您需要包含另外两行:* *xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"** 然后 IsHidden 属性完美地工作。(抱歉,这里的格式有限) (2认同)

Jay*_*y13 11

很好的解决方案,我遇到了类似的问题,我同意有些情况需要它.这是一个次要更新,允许您编辑值以在设计时打开和关闭IsHidden.如果显示控件夹点等,我还应用了ScaleTransform而不是设置Width和Height来减少屏幕伪像,并且如果隐藏的控件已经设置了Width和Height属性,则避免冲突(假设控件还没有在它上面设置LayoutTransform).

Public Class DesignModeTool

  Public Shared ReadOnly IsHiddenProperty As DependencyProperty = DependencyProperty.RegisterAttached( _
    "IsHidden", GetType(Boolean), GetType(DesignModeTool), _
    New FrameworkPropertyMetadata(False, New PropertyChangedCallback(AddressOf OnIsHiddenChanged)))

  Public Shared Sub SetIsHidden(ByVal element As FrameworkElement, ByVal value As Boolean)
    element.SetValue(IsHiddenProperty, value)
  End Sub

  Public Shared Function GetIsHidden(ByVal element As FrameworkElement) As Boolean
    Return DirectCast(element.GetValue(IsHiddenProperty), Boolean)
  End Function

  Private Shared Sub OnIsHiddenChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    If System.ComponentModel.DesignerProperties.GetIsInDesignMode(d) AndAlso True.Equals(e.NewValue) Then
      With DirectCast(d, FrameworkElement)
        .LayoutTransform = New ScaleTransform(0.001, 0.001)
      End With
    ElseIf System.ComponentModel.DesignerProperties.GetIsInDesignMode(d) AndAlso False.Equals(e.NewValue) Then
      With DirectCast(d, FrameworkElement)
        .LayoutTransform = Nothing
      End With
    End If
  End Sub
End Class 
Run Code Online (Sandbox Code Playgroud)

  • +1很好!但是我需要一个VB到C#的翻译器;-) (4认同)

gre*_*nis 8

干得好!我翻译成C#并将其更改的属性更改为RenderTransform.

static class DesignModeTool
{
    public static readonly DependencyProperty IsHiddenProperty =
        DependencyProperty.RegisterAttached("IsHidden",
            typeof(bool),
            typeof(DesignModeTool),
            new FrameworkPropertyMetadata(false,
                new PropertyChangedCallback(OnIsHiddenChanged)));

    public static void SetIsHidden(FrameworkElement element, bool value)
    {
        element.SetValue(IsHiddenProperty, value);
    }

    public static bool GetIsHidden(FrameworkElement element)
    {
        return (bool)element.GetValue(IsHiddenProperty);
    }

    private static void OnIsHiddenChanged(DependencyObject d,
                                          DependencyPropertyChangedEventArgs e)
    {
        if (!DesignerProperties.GetIsInDesignMode(d)) return;
        var element = (FrameworkElement)d;
        element.RenderTransform = (bool)e.NewValue
           ? new ScaleTransform(0, 0)
           : null;
    }
}
Run Code Online (Sandbox Code Playgroud)


Hei*_*nzi 7

由于没有内置的方法来实现这一点,我决定自己实现一个解决方案,使用附加属性非常容易:

Public Class DesignModeTool
    Public Shared ReadOnly IsHiddenProperty As DependencyProperty = DependencyProperty.RegisterAttached( _
        "IsHidden", GetType(Boolean), GetType(DesignModeTool), _
        New FrameworkPropertyMetadata(False, New PropertyChangedCallback(AddressOf OnIsHiddenChanged)))

    Public Shared Sub SetIsHidden(ByVal element As UIElement, ByVal value As Boolean)
        element.SetValue(IsHiddenProperty, value)
    End Sub

    Public Shared Function GetIsHidden(ByVal element As UIElement) As Boolean
        Return DirectCast(element.GetValue(IsHiddenProperty), Boolean)
    End Function

    Private Shared Sub OnIsHiddenChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        If System.ComponentModel.DesignerProperties.GetIsInDesignMode(d) AndAlso True.Equals(e.NewValue) Then
            With DirectCast(d, FrameworkElement)
                .Width = 0
                .Height = 0
            End With
        End If
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

声明命名空间后,可以像这样使用该功能:

<Grid ... local:DesignModeTool.IsHidden="True">
[stuff I don't want to be shown in the designer]
</Grid>
Run Code Online (Sandbox Code Playgroud)