Programmatically setting the width of a grid column with * in WPF

前端 未结 3 1005
眼角桃花
眼角桃花 2020-12-25 10:57

I want to programmatically configure a wpf grid.

I want to be able to set a grid with 2 columns, the first taking up 20% of available space, the second taking up 80

3条回答
  •  甜味超标
    2020-12-25 11:37

    MVVM Approach

    View (XAML)

    
        
            
            
        
    
    

    ViewModel (C#)

    public class MyViewModel : BindableBase
    {
        private GridLength _firstColumn;
        private GridLength _secondColumn;
    
        public MyViewModel()
        {
            _firstColumn = new GridLength(75, GridUnitType.Star);
            _secondColumn = new GridLength(25, GridUnitType.Star);
        }
    
        public GridLength FirstColumn
        {
            get { return _firstColumn; }
            set { SetProperty(ref _firstColumn, value); }
        }
    
        public GridLength SecondColumn
        {
            get { return _secondColumn; }
            set { SetProperty(ref _secondColumn, value); }
        }
    
        private void NotifyToggleFullScreen(bool isToggleExpansion)
        {
            if (isToggleExpansion)
            {
                FirstColumn = new GridLength(0, GridUnitType.Auto);
                SecondColumn = new GridLength(100, GridUnitType.Star);
            }
            else
            {
                FirstColumn = new GridLength(75, GridUnitType.Star);
                SecondColumn = new GridLength(25, GridUnitType.Star);
            }
        }
    }
    

提交回复
热议问题