【发布时间】:2011-12-18 09:26:21
【问题描述】:
我已经定义了一个依赖属性如下:
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); }
}
当我的应用程序启动时,我会这样做......
private void Window_Loaded(object sender, RoutedEventArgs e)
{
AnimateColumnWidth = Properties.Settings.Default.ProductInfoWidthExpanded;
}
...应该将值初始化为其起始值 - 在本例中为 400。
然后,我将 UI 中的一列网格绑定到此属性...
<ColumnDefinition
Name="ProductInfo"
Width="{Binding Path=AnimateColumnWidth,
Converter={StaticResource doubleToGridLength},
Mode=TwoWay}" />
据我了解,由于列宽绑定到此属性,所以每当我更新该属性时,列宽也应该更新。
当我更改属性时宽度没有更新,我做错了什么?我也试图通过一个也不起作用的动画来更新它。此外,在 AnimateColumnWidth 属性的 getter 上设置的断点永远不会被命中 - 这意味着从未尝试检索该属性。
(这确实有效,我在某个地方弄坏了一些东西!)
脚注:
转换的值是在我的应用程序的根命名空间中定义的(我相信如果 WPF 找不到它会抱怨)。
[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;
}
}
【问题讨论】:
标签: c# .net wpf xaml dependency-properties