.Net Maui converter in ColumnDefinition

1.5k views Asked by At

In my view I have a Grid with a number of columns. I would like to hide/show one column and resize the others based on an ObservableProperty in the view model.

I have tried to bind to the Width of the ColumnDefinition and use a converter to convert the boolean to a GridLength. But no matter how I try, my breakpoint in the converter is not hit.

Is it simply not possible? a bug? or have I just not found the correct syntax yet?

My latest attempt looks like this:

<ColumnDefinition Width="{Binding ShowColorColumn, Converter={StaticResource converters:BoolToGridLengthConverter}, ConverterParameter='80|0'}" />
1

There are 1 answers

2
Adrain On BEST ANSWER

You can bind the ColumnDefinitions with ColumnDefinitionCollection, and add several ColumnDefinition object into it. like:

<Grid  ColumnDefinitions="{Binding columns}">



        <Label Grid.Row="0" Grid.Column="0" BackgroundColor="Red" Text="Label1"/>
        <Label Grid.Row="0" Grid.Column="1" BackgroundColor="Yellow" Text="Label2"/>


        <Label Grid.Row="0" Grid.Column="2" BackgroundColor="Green" Text="Label3"/>
    </Grid>

code bdehind:

public ColumnDefinitionCollection columns { set; get; }
    public string text { set; get; }
    public GridTestPage()
    {
    
        columns = new ColumnDefinitionCollection();
    ColumnDefinition column1=new ColumnDefinition();
        ColumnDefinition column2 = new ColumnDefinition();
        ColumnDefinition column3 = new ColumnDefinition();
        column1.Width = 20;
        column2.Width = 40;
        column3.Width = 60;
        
        columns.Add(column1);
        columns.Add(column2);
        columns.Add(column3);
    
        InitializeComponent();
        
        
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        BindingContext = this;
    }