2011-10-30 161 views
2

我已經定義了一個依賴屬性格式如下:依賴屬性綁定未更新

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.

然後我在我的用戶界面中綁定了一列網格到這個屬性...

<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; 
    } 
} 

回答

0

我沒有做的一件事是設置網格的datacontext誰是我想影響「這個」的列。

public MainWindow() 
{ 
    InitializeComponent(); 
    ProductsArea.DataContext = this; 
} 
5

您註冊性質爲"AnimateColumnWidthProperty",這是「錯誤的」,只有字段名是任意的,你可能想"AnimateColumnWidth"(或更改課程的結合,但由於是,它由於路徑點失敗到未註冊的財產)。

您可能還想閱讀關於調試bindings的內容,然後您可以發現這些錯誤,因爲它們將由綁定引擎進行報告。 (類似於「在對象y上找不到屬性x」)。

在getters或setters中也使用斷點不會告訴你任何事情,綁定引擎確實使用而不是,它們只是爲了您的方便。

+0

+1用於鏈接到msdn調試綁定鏈接 –

+0

綁定提示做了訣竅。網格的datacontext尚未設置。 – Remotec