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(); 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 });
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.