2014-10-10 29 views
0

我有一個可能非常愚蠢的問題與WPF中的數據綁定。我有一個類型的自定義異常WPF - 如何正確地綁定子類屬性

public class MyException : Exception 
{ 
    public class ThrowingMethod 
    { 
     public class RegisteredMethod 
     { 
      public readonly string registeredName; 

      // ... 
     } 

     public readonly RegisteredMethod regMethod; 

     // ... 
    } 

    public readonly ThrowingMethod throwingMethod; 
    // ... 
} 

權,現在在我的WPF窗口,我做

public partial class ExceptionDisplay : Window 
{ 
    private readonly IEnumerable<MyException> exceptions; 

    public ExceptionDisplay(IEnumerable<MyException> exceptions) 
    { 
     InitializeComponent(); 
     this.exceptions = exceptions; 

    } 

    private void Window_Loaded_1(object sender, RoutedEventArgs e) 
    { 
     DataContext = this.exceptions.First(); 
    } 
} 

而且我在XAML兩個標籤:

<Label Name="Message" Content="{Binding Message}"/> 

<Label Name="RegMethod" Content="{Binding Path=throwingMethod.regMethod.registeredName, Mode=OneWay}"/> 

奇怪的是,消息結合正確,但第二個標籤仍然是空的,沒有任何東西似乎與其綁定。 當我通過Window_Loaded_1中的代碼手動設置值時,它工作得很好,所以對象都被正確初始化。

爲什麼throwingMethod.regMethod.registeredName不能綁定,我在這裏做錯了什麼?

謝謝!

回答

1

你可以綁定到屬性而不是類的字段。

你至少可以寫:

public ThrowingMethod throwingMethod { get; private set; } 

同爲registeredNameregMethod

下一個問題是如果你需要更新。所以INotifyPropertyChanged接口會很有用。

+0

謝謝,這工作!感謝INotifyPropertyChanged建議以及,但不會需要 - 我只會顯示一些例外。一旦拋出,他們將不需要進一步更新。 – Bogey 2014-10-10 08:33:34