Hide grid row in WPF

前端 未结 8 1447
旧巷少年郎
旧巷少年郎 2020-12-02 15:21

I have a simple WPF form with a Grid declared on the form. This Grid has a bunch of rows:


    

        
相关标签:
8条回答
  • 2020-12-02 15:26

    Instead of fiddling with the Grid Row, you can set the Visibility property of the Controls (fields in the row) to "Collapsed". This will ensure that the controls don't take up any space and if you have Grid Row Height="Auto", then the row will be hidden as all the controls in the row have Visibility="Collapsed".

    <Grid>
           <Grid.RowDefinitions>
             <RowDefinition Height="Auto" />
             <RowDefinition Height="Auto" Name="rowToHide" />
           </Grid.RowDefinitions>
    
       <Button Grid.Row=0 Content="Click Me" Height="20">
           <TextBlock Grid.Row=1 
    Visibility="{Binding Converter={StaticResource customVisibilityConverter}}" Name="controlToHide"/>
    
    </Grid>
    

    This method is better because the Visibility of the controls can be bound to some property with the help of a Converter.

    0 讨论(0)
  • 2020-12-02 15:29

    Simply do this:
    rowToHide.Height = new GridLength(0);

    if u will use visibility.Collapse then u have to set it for every member of the row.

    0 讨论(0)
  • 2020-12-02 15:30

    Row does not have a Visibility property, so as others have said, you need to set the Height. Another option is to use a converter, in case you need this functionality in many views:

        [ValueConversion(typeof(bool), typeof(GridLength))]
        public class BoolToGridRowHeightConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ((bool)value == true) ? new GridLength(1, GridUnitType.Star) : new GridLength(0);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {    // Don't need any convert back
                return null;
            }
        }
    

    And then in the appropriate view <Grid.RowDefinition>:

    <RowDefinition Height="{Binding IsHiddenRow, Converter={StaticResource BoolToGridRowHeightConverter}}"></RowDefinition>
    
    0 讨论(0)
  • 2020-12-02 15:30

    For reference, Visibility is a three-state System.Windows.Visibility enumeration:

    • Visible - The element gets rendered and participates in layout.
    • Collapsed - The element is invisible and does not participate in layout. Effectively giving it a height and width of 0 and behaving as if it doesn't exist.
    • Hidden - The element is invisible but continues to participate in layout.

    See this tip and other tips on the WPF Tips and Tricks thread.

    0 讨论(0)
  • 2020-12-02 15:33

    I had a similar idea by inheriting RowDefinition (just for interest)

    public class MyRowDefinition : RowDefinition
    {
        private GridLength _height;
    
        public bool IsHidden
        {
            get { return (bool)GetValue(IsHiddenProperty); }
            set { SetValue(IsHiddenProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for IsHidden.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsHiddenProperty =
            DependencyProperty.Register("IsHidden", typeof(bool), typeof(MyRowDefinition), new PropertyMetadata(false, Changed));
    
        public static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var o = d as MyRowDefinition;
            o.Toggle((bool)e.NewValue);
        }
    
        public void Toggle(bool isHidden)
        {
            if (isHidden)
            {
                _height = this.Height;
                this.Height = new GridLength(0, GridUnitType.Star);
            }                                                     
            else
                this.Height = _height;
        }          
    }
    

    Now you can use it as following:

     <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <my:MyRowDefinition Height="4*" IsHidden="false" x:Name="RowToHide" />
            <RowDefinition Height="*" />
            <RowDefinition Height="60" />
        </Grid.RowDefinitions>
    

    and toggle with

    RowToHide.IsHidden = !RowToHide.IsHidden;
    
    0 讨论(0)
  • 2020-12-02 15:45

    You can also do this by referencing the Row in the Grid and then changing the Height of the row itself.

    XAML

    <Grid Grid.Column="2" Grid.Row="1" x:Name="Links">
       <Grid.RowDefinitions>
          <RowDefinition Height="60" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="80" />
       </Grid.RowDefinitions>
    </Grid>
    

    VB.NET

    If LinksList.Items.Count > 0 Then
       Links.RowDefinitions(2).Height = New GridLength(1, GridUnitType.Star)
    Else
       Links.RowDefinitions(2).Height = New GridLength(0)
    End If
    

    Whilst the Collapsing of the elements within the Grid also works, this is a bit simpler if you have many items in the Grid that does not have an enclosing element that can be collapsed. This would provide a good alternative.

    0 讨论(0)
提交回复
热议问题