none
How to remove the height dynamically in WPF?

    Question

  • hi,

    In one of our Screen, we are dynamically hiding listview.

    We have 3 rows.

    <

     

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

     

     

    <RowDefinition Height="200"/>

     

     

    </Grid.RowDefinitions>

    In all the times Row 0 and Row 2 will be displayed. But in some cases the Row 1(i.e listview row) should be displayed .

    In particular situation listview will be collapsed. When we give Row 1 as "*" then if the listview is collapsed that time also its taking the height its not shrinking. so Row 1 is set to Auto.

    It is collapsing now. But the issue is when the grid has more rows then the Row 2 is  not visible completely. It is partially visible.

    How do we show the 3 rows if the Listview is visible and remove the row 1 height if the listview is not there?

    Main purpose is the last row should come up if listview is not visible.

    thanks in advance.

    please provide some code or suggestion.

    Monday, March 15, 2010 12:35 PM

Answers

  • Hi, you could use a converter and bind the height to the listviews Visibility:

    XAML:

    <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="70"/>
                <RowDefinition Height="{Binding Visibility, ElementName = lw, Converter={StaticResource VisibilityToHeight}}"/>
                <RowDefinition Height="70"/>
            </Grid.RowDefinitions>
            
            <TextBlock Grid.Row="0" Text="Header" />
            
            <ListView x:Name="lw" Grid.Row="1" Visibility="Collapsed" />
            
            <Button Grid.Row="2" Content="Button" />
        </Grid>
    The converter:

    public class VisibilityToHeight : IValueConverter
        {
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (!(value is Visibility)) return GridUnitType.Auto;
    
                Visibility v = (Visibility)value;
                
                return v == Visibility.Collapsed ? (object)GridLength.Auto : new GridLength(1.0,GridUnitType.Star);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    /Joakim
    Monday, March 15, 2010 4:32 PM

All replies

  • Hi, you could use a converter and bind the height to the listviews Visibility:

    XAML:

    <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="70"/>
                <RowDefinition Height="{Binding Visibility, ElementName = lw, Converter={StaticResource VisibilityToHeight}}"/>
                <RowDefinition Height="70"/>
            </Grid.RowDefinitions>
            
            <TextBlock Grid.Row="0" Text="Header" />
            
            <ListView x:Name="lw" Grid.Row="1" Visibility="Collapsed" />
            
            <Button Grid.Row="2" Content="Button" />
        </Grid>
    The converter:

    public class VisibilityToHeight : IValueConverter
        {
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (!(value is Visibility)) return GridUnitType.Auto;
    
                Visibility v = (Visibility)value;
                
                return v == Visibility.Collapsed ? (object)GridLength.Auto : new GridLength(1.0,GridUnitType.Star);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    /Joakim
    Monday, March 15, 2010 4:32 PM
  • Hi Rain drops,

    Have you checked out jocke82's post? did it make sense to you?

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    Tuesday, March 16, 2010 1:08 PM
  • Hi Jim,

    sorry for the late reply.

    I have checked this code it is working fine.

    thanks for the solution.


    One more clarification required like can we pass the required height as param to converter from xaml?



    Regards,

    Rain drops
    Thursday, March 18, 2010 4:14 AM
  • Hi,

    You can. you can specify the ConverterParameter property in XAML markup. If you are still having any issues with this case, please feel free to ask.

    Thanks.

    Sincerely.


    Jim Zhou -MSFT
    Monday, March 22, 2010 12:36 PM