2014-10-10 81 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