How do I databind a ColumnDefinition's Width or RowDefinition's Height?

后端 未结 4 1467
南旧
南旧 2020-12-03 02:54

Under the View-Model-ViewModel pattern for WPF, I am trying to databind the Heights and Widths of various definitions for grid controls, so I can store the values the user s

4条回答
  •  生来不讨喜
    2020-12-03 03:16

    Create a IValueConverter as follows:

    public class GridLengthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double val = (double)value;
            GridLength gridLength = new GridLength(val);
    
            return gridLength;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            GridLength val = (GridLength)value;
    
            return val.Value;
        }
    }
    

    You can then utilize the converter in your Binding:

    
        
    
    ...
    
    

提交回复
热议问题