WPF snap to grid column width

I am trying to associate DependancyProperty in one of my usercontrols with the Width property of the Column in the Grid .

I have code similar to this:

 <Grid x:Name="MyGridName"> <Grid.ColumnDefinitions> <ColumnDefinition x:Name="TitleSection" Width="100" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions>...</Grid.RowDefinitions> <GridSplitter x:Name="MyGridSplitter" Grid.Row="0" Grid.Column="0" ... /> </Grid> 

In a separate Usercontrol, I have the following DependancyProperty .

 public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.Register("TitleWidth", typeof(int), typeof(MyUserControl)); public int TitleWidth { get { return (int)base.GetValue(TitleWidthProperty); } set { base.SetValue(TitleWidthProperty, value); } } 

I am creating Usercontrol instances in the code, so I have a binding statement like this:

 MyUserControl Cntrl = new MyUserControl(/* Construction Params */); BindingOperations.SetBinding(Cntrl , MyUserControl.AnotherProperty, new Binding { ElementName = "objZoomSlider", Path = new PropertyPath("Value"), Mode = BindingMode.OneWay }); BindingOperations.SetBinding(Cntrl , MyUserControl.TitleWidthProperty, new Binding { ElementName = "TitleSection", Path = new PropertyPath("ActualWidth"), Mode = BindingMode.OneWay }); /* Other operations on Cntrl */ 

The first related definition works fantastically, although it is related to the actual UIElement (in this case, Slider), but the Binding on the β€œTitleSection” (which is the ColumnDefinition definition defined in the Grid) fails. Putting the checkpoint in the code and executing the clock on "TitleSection" returns the expected object.

I'm starting to suspect that the x: Name'd ColumnDefinition constraint cannot be bound. Can anyone suggest how I can snap to the varying width of the first column in my grid?

EDIT # 1 - reply to comments

Binding data is a failure in the sense that with a breakpoint set on the installer for the TitleWidth property, and using the GridSplitter control to resize the first column, the breakpoint never hits. Also, the code that I would expect to get fired when the DependancyProperty TitleWidth changes TitleWidth not implemented.

A user control is created and added to the Stackpanel inside the grid in the Window_Loaded function. I would expect a Grid to be provided by the time Usercontrols is created. Of course, the x: Name'd TitleSection TitleSection is observable and has a value of 100 when they are created / before the binding occurs.

EDIT # 2 - Maybe something has to do with this?

I sniffed the MSDN pages for the Grid ColumnDefinition documentation and met GridLength () , but I can’t hug how I can use this in a binding expression. I cannot use the associated GridLengthConverter as a converter in the binding code since it is not derived from IValueConverter.

I am inclined to some kind of binding to the ActualWidth property of one of the cells in the Grid object. It doesn't seem as clean as binding to a column definition, but at the moment I can't get this to work.

+7
wpf grid
source share
3 answers

Well, I have a bit of work, I will explain how for future generations:

Essentially, I have a grid with two columns, several rows with a separator aligned to the right in the first column, so the user can resize it if the content contained in it requires more space. To complicate the situation, I have a user control that is programmatically loaded into some rows that use a column from 2nd place (the content "expires" from one cell to the next).

When the first column changes, I need it to be reflected in usercontrol. First, I tried to snap to ColumnDefinition, but he really didn't play the ball.

How I fixed / blocked it

In the backup cell in the first column, I added <Label> with x: Name to make it available. Since it is in a cell, it has the default 'Stretch' properties and completely fills the cell. It changes when you resize a column using a separator. Binding to the Label property ActualWidth means that column resizing is reported to DependancyProperty in my column. Properly configured user controls.

Thoughts

Obviously, even though the ColumnDefinition has the ActualWidth property when it changes, it does not seem to trigger the PropertyChanged event internally (or this is my best guess). It may be a mistake or design, but for me it means that I had to use a less clean solution.

+9
source share

You tried to configure binding in xaml, the following should work for you.

 <ColumnDefinition x:Name="TitleSection" Width="{Binding Path=TitleWidth, RelativeSource={RelativeSource AncestorType=MyUserControl}}" /> 

Otherwise, looking at the binding code that you provided, it looks wrong to me, the purpose of the binding should be a grid column, and the source should be your dependency property.

The equivalent code for the above xaml is

 BindingOperations.SetBinding(TitleSection, ColumnDefinition.WidthProperty, new Binding() { RelativeSource= new RelativeSource(RelativeSourceMode.FindAncestor, typeof(MyUserControl),1), Path = new PropertyPath("TitleWidth"), }); 

In a related note, the use of the GridSplitter and the binding of the Width property are mutually exclusive. Once you resize the column using the separator, your binding will be replaced with the value from the separator.

This is similar to what you experienced by updating any property that is a binding object. When you set the value of the target directly in the code, you will essentially change the binding object to the value that you specify.

+3
source share

ColumnDefinition.Width is not an integer - it is a GridLength. You cannot bind GridLength directly to Integer, you need a converter.

This is also the reason why you cannot bind any Control Width (double) property to the ColumnDefinition Width (GridLength) property without a converter.

+2
source share

All Articles