代码之家  ›  专栏  ›  技术社区  ›  Heinzi

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

  •  28
  • Heinzi  · 技术社区  · 16 年前

    我有一个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>
    

    ,问题是Visual Studio忽略了 Visibility="Hidden" 通常,这很有道理(毕竟,我希望能够编辑隐藏的UI元素),但就我而言,这很烦人,因为它阻止了我在设计器中编辑DockPanel中的内容。

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

    <Grid Background="#CCCC" Visibility="Hidden" VS.ShowInDesigner="False">
      [overlay grid which is only shown during special circumstances]
    </Grid>
    

    但是,唉,没有这样的财产,或者至少据我所知没有。有什么想法吗?

    8 回复  |  直到 16 年前
        1
  •  11
  •   Jay13    16 年前

    • 如果还没有,请添加xmlns:d=“http://schemas.microsoft.com/expression/blend/2008"
        2
  •  61
  •   adospace    12 年前

    不错的解决方案,我也遇到了类似的问题,我同意在某些情况下需要它。这是一个小更新,允许您在设计时编辑值以打开和关闭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 
    
        3
  •  7
  •   Heinzi    16 年前

    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;
        }
    }
    
        4
  •  7
  •   gregsdennis    13 年前

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

    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
    

    <Grid ... local:DesignModeTool.IsHidden="True">
    [stuff I don't want to be shown in the designer]
    </Grid>
    
        5
  •  3
  •   Tim Cooper    14 年前

    除了不使用设计器(真的,考虑一下这个),您还可以将 Grid 变成一个单独的 UserControl 那样的话,你可以更新一下

        6
  •  2
  •   mittio    15 年前

    我使用矩形在模态对话框执行期间遮挡主窗口。我绑定了可见性数据,但矩形使设计器无法使用。我将Z索引设置为一次性数据绑定,回退值低于我想要隐藏的窗口。当应用程序启动时,矩形的Z索引绑定到比窗口更高的值。

        7
  •  1
  •   drongal    12 年前

    我在另一边。..讨厌VS 2012在设计器中隐藏隐藏的WPF控件。我需要看到它们,所以我将gregsdennis代码修改为:

    public 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.Visibility=Visibility.Visible;
    
        }
    }
    

    wpfClasses2:DesignModeTool。IsHidden=“False”将在设计器模式下显示控件。