Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding to Grid Column Width

Tags:

wpf

grid

I'm attempting to bind a DependancyProperty in one of my usercontrols to the Width property of a Column in a 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 followingDependancyProperty defined.

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 instances of the Usercontrol in code, hence I have a binding statement similar to 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 binding defined works fantastically, although that is binding to an actual UIElement (in this case a Slider), but the Binding to "TitleSection" (which is the ColumnDefinition defined in the Grid) fails. Putting a breakpoint in the code and doing a watch on "TitleSection" returns the expected object.

I am beginning to suspect that a x:Name'd ColumnDefinition can't be bound to. Can anyone suggest how I might be able to bind to the changing width of the first column in my grid?

EDIT #1 - To answer comments

The databinding 'fails' in the sense that with a breakpoint set on the setter for the TitleWidth property, and using the GridSplitter control to resize the first column, the breakpoint is never hit. Additionally, code I would expect to be fired when the DependancyProperty TitleWidth changes does not get executed.

The usercontrol is being created and added to a Stackpanel within the Grid in the Window_Loaded function. I would expect that the Grid has been rendered by the time the Usercontrols are being constructed. Certainly the x:Name'd Element TitleSection is watchable and has a value of 100 when they are being constructed / before the binding is happening.

EDIT #2 - Possibly something to do with this?

I've been having a sniff round the MSDN pages for the Grid ColumnDefinition documentation and have come across GridLength() but I can't get my head around how I can use this in a binding expression. I cannot use the associated GridLengthConverter as a converter in the binding code as it does not derive from IValueConverter.

I am leaning towards somehow binding to the ActualWidth property of one of the cells in the Grid object. It doesn't seem as clean as binding to the column definition, but at the moment I cannot get that to work.

like image 615
Ash Avatar asked Nov 12 '08 23:11

Ash


1 Answers

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

This is also the reason, why you can't bind any Control's Width property (double) to a ColumnDefinition's Width property (GridLength) without a converter.

like image 123
JCH2k Avatar answered Oct 18 '22 17:10

JCH2k