Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide column or row when content is not visible

So I have a xaml Grid with columns and I would like to hide or collapse the column when the content is not visible.

example : I have this layout :

<Grid >      
    <Button Grid.Column="0" x:Name="FirstButton"  Text="First button" />      
    <Button Grid.Column="1"x:Name="SecondButton"  Text="Second button" />
</Grid>

When FirstButton is not visible I want this result

  <Grid >      
    <Button Grid.Column="1"x:Name="SecondButton"  Text="Second button" />
</Grid>
like image 436
Renaud Avatar asked Apr 10 '17 12:04

Renaud


People also ask

How do you hide rows or columns with an easily visible expand?

Groups and outlines allow you to quickly hide and unhide rows or columns in an Excel spreadsheet. The Groups feature creates row and column groupings in the Headings section of the worksheet. Each group can be expanded or collapsed with the click of a button.


1 Answers

Answering myself :

  <Grid>      
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding Path=IsVisible, Converter={StaticResource IsVisibleToGridLength}" BindingContext="{x:Reference FirstButton}"   />
        <ColumnDefinition Width="{Binding Path=IsVisible, Converter={StaticResource IsVisibleToGridLength}" BindingContext="{x:Reference SecondButton}" />
  </Grid.ColumnDefinitions>
        
  <Button Grid.Column="0" x:Name="FirstButton"  Text="First button" />      
  <Button Grid.Column="1"x:Name="SecondButton"  Text="Second button" />
</Grid>

And for the converter part

class IsVisibleToGridLengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo language)
    {
        try
        {
            GridUnitType t = GridUnitType.Star;
            if (parameter != null)
            {
                Enum.TryParse<GridUnitType>((string)parameter, true, out t);                    
            }

            if (value != null)
            {
                bool d = (bool)value;
                return d == false ? new GridLength(0,GridUnitType.Absolute) : new GridLength(1, t);
            }
            return null;
        }
        catch (Exception exp)
        {                
            return null;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)
    {
        return null;
    }
}

And Obviously the App.xml part

<Application  x:Class="MyNameSpace.App"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:class="clr-namespace:MyNameSpace.Class;assembly=MyNameSpace"/>
<Application.Resources>
  <ResourceDictionary>     
    <class:IsVisibleToGridLengthConverter  x:Key="IsVisibleToGridLength"/>   
  </ResourceDictionary>
</Application.Resources>
</Application>

Hope it helps !!

like image 172
Renaud Avatar answered Oct 13 '22 00:10

Renaud