I have defined a dependency propery as follows:
public static readonly DependencyProperty AnimateColumnWidthProperty =
DependencyProperty.Register("AnimateColumnWidthProperty", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0));
public double AnimateColumnWidth
{
get { return (double)GetValue(AnimateColumnWidthProperty); }
set { SetValue(AnimateColumnWidthProperty, value); }
}
When my application starts I do this….
private void Window_Loaded(object sender, RoutedEventArgs e)
{
AnimateColumnWidth = Properties.Settings.Default.ProductInfoWidthExpanded;
}
… which should initialise the value to its starting value – in this case 400.
I have then bound a column of a grid in my UI to this property…
<ColumnDefinition
Name="ProductInfo"
Width="{Binding Path=AnimateColumnWidth,
Converter={StaticResource doubleToGridLength},
Mode=TwoWay}" />
As I understand it, as the column width is bound to this property, whenever I update the property the column width should also update.
What am I doing wrong as the width does not update when I change the property? I am also trying to update it via an animation which also doesnt work. Additionally, a breakpoint set on the getter of the AnimateColumnWidth property is never hit – meaning that nothing ever tried to retrieve the property.
(This did work so clearly I have broken something somewhere!!)
Footnote:
The value converted is defined in the root namespace of my app (I believe that WPF would complain if it couldnt find it).
[ValueConversion(typeof(Double), typeof(GridLength))]
public class DoubleToGridLength : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new GridLength((double)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((GridLength)value).Value;
}
}
One thing I hadnt done was set the datacontext of the grid who’s column I wanted to affect to “this”.