locked
How to bind column width to variable in another class. RRS feed

  • Question

  • User392332 posted

    Hi everybody :) I'm trying to bind width in a columndefinition to a variable in another class. However, I can't seem to get the binding right.... Anybody knows how to do this?

    XAML-file:

     <Grid HorizontalOptions="FillAndExpand" ColumnSpacing="0" RowSpacing="0" Grid.Row="1" VerticalOptions="FillAndExpand">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Grid.Row="0" x:Name="colDef" 
                                          **Width="{Binding Globals.MenuWidth}"/>**
                    </Grid.ColumnDefinitions>
    

    Another class:

    static class Globals
        {
             public static GridLength MenuWidth= new GridLength(6, GridUnitType.Star);
        }
    

    Friendly regards nbs

    Tuesday, May 19, 2020 8:14 PM

Answers

  • User369979 posted

    Try to create a utility class like:

    public class Globals : INotifyPropertyChanged
    {
        public static Globals Instance = new Globals();
    
        GridLength menuWidth = new GridLength(6, GridUnitType.Star);
        public GridLength MenuWidth
        {
            get => menuWidth;
            set
            {
                menuWidth = value;
                onProperyChanged();
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        void onProperyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    And then we could consume it like:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="{Binding MenuWidth, Source={x:Static local:Globals.Instance}}"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    
        <Button Text="Change" Clicked="Button_Clicked"/>
        <BoxView Grid.Column="1" BackgroundColor="Red"/>
        <BoxView Grid.Column="2" BackgroundColor="Green"/>
    
    </Grid>
    

    Here is the effect:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 20, 2020 2:43 AM

All replies

  • User369979 posted

    Try to create a utility class like:

    public class Globals : INotifyPropertyChanged
    {
        public static Globals Instance = new Globals();
    
        GridLength menuWidth = new GridLength(6, GridUnitType.Star);
        public GridLength MenuWidth
        {
            get => menuWidth;
            set
            {
                menuWidth = value;
                onProperyChanged();
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        void onProperyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    And then we could consume it like:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="{Binding MenuWidth, Source={x:Static local:Globals.Instance}}"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    
        <Button Text="Change" Clicked="Button_Clicked"/>
        <BoxView Grid.Column="1" BackgroundColor="Red"/>
        <BoxView Grid.Column="2" BackgroundColor="Green"/>
    
    </Grid>
    

    Here is the effect:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 20, 2020 2:43 AM
  • User392332 posted

    Thank You very much for Your help LandLu. This works perfectly B) You saved my day :)

    Friendly regards nbs

    Wednesday, May 20, 2020 12:42 PM