[英]WPF - How to databind subclass property correctly
I have a probably very stupid problem with Databinding in WPF. 我在WPF中使用数据绑定可能有一个非常愚蠢的问题。 I have a custom exception of type 我有一个类型的自定义例外
public class MyException : Exception
{
public class ThrowingMethod
{
public class RegisteredMethod
{
public readonly string registeredName;
// ...
}
public readonly RegisteredMethod regMethod;
// ...
}
public readonly ThrowingMethod throwingMethod;
// ...
}
Right, now in my wpf window, I do 对,现在在我的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();
}
}
And I have two labels in XAML: 我在XAML中有两个标签:
<Label Name="Message" Content="{Binding Message}"/>
<Label Name="RegMethod" Content="{Binding Path=throwingMethod.regMethod.registeredName, Mode=OneWay}"/>
oddly, Message binds correctly, but the second label remains empty and nothing seems to bind to it. 奇怪的是,Message正确绑定,但是第二个标签仍然为空,似乎没有任何绑定。 When I set the values manually by code in Window_Loaded_1, it works just fine, so the objects are all initialized correctly. 当我通过Window_Loaded_1中的代码手动设置值时,它可以正常工作,因此所有对象均已正确初始化。
Why does throwingMethod.regMethod.registeredName not bind, am I doing something wrong here? 为什么throwingMethod.regMethod.registeredName不绑定,我在这里做错了吗?
Thanks! 谢谢!
You just can bind to properties not fields of a class. 您只能绑定到属性,而不能绑定到类的字段。
You at least have to write: 您至少必须写:
public ThrowingMethod throwingMethod { get; private set; }
Same for registeredName
and regMethod
. 与registeredName
和regMethod
相同。
Next question is if you need updating. 下一个问题是是否需要更新。 So INotifyPropertyChanged interface would be useful. 因此,INotifyPropertyChanged接口将很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.