Controlling the Name/Value width ratio of the property grid

Grids for WPF Forum

Posted 10 years ago by Gustavo Guerra
Version: 4.5.0486
Avatar
How can I get/set the widths of the name and value columns of the propertygrid?
I want to set a different default at startup (30% Name, 70%Value) and then store that value as a setting between different sessions.

Best Regards,
Gustavo Guerra

Comments (4)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Gustavo,

The width of the columns are defined as GridLength values and can be set or retrieved via the PropertyGrid.Columns collection. The name column is at index 0, and the value column is at index 1. So you can do something like this to initialize the widths as you described above:
this.propGrid.Columns[0].Width = new GridLength(3, GridUnitType.Star);
this.propGrid.Columns[1].Width = new GridLength(7, GridUnitType.Star);


Actipro Software Support

Posted 10 years ago by Gustavo Guerra
Avatar
Thank you.

How can I bind to that value? I've tried the following:

Binding captionBinding = new Binding("CaptionWidth");
captionBinding.Mode = BindingMode.TwoWay;
captionBinding.Source = this;
propertyGrid.Columns[0].GetColumnDefinition().SetBinding(ColumnDefinition.WidthProperty, captionBinding);

Binding valueBinding = new Binding("ValueWidth");
valueBinding.Mode = BindingMode.TwoWay;
valueBinding.Source = this;
propertyGrid.Columns[1].GetColumnDefinition().SetBinding(ColumnDefinition.WidthProperty, valueBinding);


public static readonly DependencyProperty CaptionWidthProperty =
DependencyProperty.Register("CaptionWidth", typeof(GridLength), typeof(Properties),
new FrameworkPropertyMetadata(new GridLength(1, GridUnitType.Star),
new PropertyChangedCallback(OnCaptionWidthChanged)));

public static readonly DependencyProperty ValueWidthProperty =
DependencyProperty.Register("ValueWidth", typeof(GridLength), typeof(Properties),
new FrameworkPropertyMetadata(new GridLength(100, GridUnitType.Pixel),
new PropertyChangedCallback(OnValueWidthChanged)));

public GridLength CaptionWidth {
get { return (GridLength)GetValue(CaptionWidthProperty); }
set { SetValue(CaptionWidthProperty, value); }
}

public GridLength ValueWidth {
get { return (GridLength)GetValue(ValueWidthProperty); }
set { SetValue(ValueWidthProperty, value); }
}

private static void OnCaptionWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
}

private static void OnValueWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
}

But OnCaptionWidthChanged & OnValueWidthChanged are never called.
What am I doing wrong?

Best Regards,
Gustavo Guerra
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Gustavo,

You need to bind to the Width property of the TreeListViewColumn object, like so:
Binding valueBinding = new Binding("ValueWidth");
valueBinding.Mode = BindingMode.TwoWay;
valueBinding.Source = this;
BindingOperations.SetBinding(this.propGrid.Columns[1], TreeListViewColumn.WidthProperty, valueBinding);
Keep in mind that the width of the name column will not be changed. The size of the value column is sized to give or take more room.


Actipro Software Support

Posted 10 years ago by Gustavo Guerra
Avatar
Thank you.
The latest build of this product (v2018.1 build 0675) was released 7 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.