locked
HIde/Collapse Row or Column RRS feed

  • Question

  • Hi WPF,

        <Grid.RowDefinitions>
            <RowDefinition x:Name="Row1" />
            <RowDefinition x:Name="Row2" />
        </Grid.RowDefinitions>

    Row2.Visibility = Visibility.Hidden;

    How to i hide or collapse 2nd row in wpf?

    Thanks

    Friday, July 3, 2015 8:18 AM

Answers

  • If you set the height to auto and it has something in it then that's not going to work.

    You can set the height of the row.

    Row2.Height = new GridLength(0);

    and back

    Row2.Height = new GridLength(1, GridUnitType.Star);


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Marked as answer by IamGuy84 Monday, July 6, 2015 5:36 AM
    Friday, July 3, 2015 9:47 AM

All replies

  • Hi lamGuy,

    you should set the Height of the RowDefinition to "Auto".

    Then it automatically takes the height of the highest element in that row. If you remove the element or set its Visibility to Collapsed, the row will also take not that full height anymore.

    <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

    If this does not work for you, you could as well dynamically remove a RowDefinition from the Grid and re-add it again if you want. But note: Removing a RowDefinition does not remove the Elements from the Grid. So the Elements in that row will just be placed in the RowDefinition nearby with the same index.


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-courses:
    XAML Layout in Depth
    Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com

    Friday, July 3, 2015 9:02 AM
  • If you set the height to auto and it has something in it then that's not going to work.

    You can set the height of the row.

    Row2.Height = new GridLength(0);

    and back

    Row2.Height = new GridLength(1, GridUnitType.Star);


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Marked as answer by IamGuy84 Monday, July 6, 2015 5:36 AM
    Friday, July 3, 2015 9:47 AM
  • >>How to i hide or collapse 2nd row in wpf?

    You set the Visibility property of all elements in the second row to Collapsed or you set the Height property of the RowDefinition to 0 so that it doesn't take up any space.

    It is important to realize that the RowDefinition itself is not an element that gets added to the visual tree so you cannot collapse all elements in the second row by just setting some Visbility property of a RowDefinition.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    Friday, July 3, 2015 1:56 PM