Hide WPF elements in Visual Studio designer

后端 未结 7 1848
暗喜
暗喜 2020-11-30 08:39

I have a WPF form which basically looks like this:


  
    
      [content shown during normal operation]
             


        
相关标签:
7条回答
  • 2020-11-30 09:29

    Nice solution, I was having a similar problem and I agree that there are cases where it's needed. Here is a minor update that allows you to edit the value to turn IsHidden on and off while designing. I also applied a ScaleTransform instead of setting Width and Height to reduce screen artifacts a bit if control grips etc are displayed and to avoid conflicts if the control being hidden already has Width and Height properties set (assuming that the control doesn't already have a LayoutTransform set on it).

    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 
    
    0 讨论(0)
提交回复
热议问题