WPF 与 LINQ 类的绑定问题
我有一个 WPF 页面 (ZorgTrajectPage1.xaml),其背后的代码为 ZorgTrajectPage1.xaml.cs,其中包含一个名为 Ztc 的 DependencyProperty ZorgtrajectController。
dataContext 在 ZorgTrajectPage1.xaml 的 Page_Loaded() 中设置:
Ztc = new ZorgTrajectController();
DataContext = Ztc;
看来我可以通过绑定访问此 ZorgTrajectController 以查找此控制器中的整数(患者 ID 变量):
<TextBox Name="textBox1" Text="{Binding Path=PatientID, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
这非常有效。但我还有一个选择 educatiePakket 的组合框。做出此选择后,将通过 LINQ 查询查找 selectedEducatiePakket 并将其放入 ZorgTrajectController 中的变量中。此 SelectedEducatiePakket 是 LINQ 类的新实例(该类具有一个带有其名称的字符串属性,称为 naam)。我使用以下方法来填充它:
SelectedEducatiepakket = SelectedEducatiePakketByID(5);
public educatiepakket SelectedEducatiePakketByID(int id)
{
educatiepakket ep = (from o in db.educatiepakkets
where o.educatiepakket_id == id
select o).Single() as educatiepakket;
return ep;
}
以下:
<TextBox Name="EPNaamTxtbx" Text="{Binding Path=SelectedEducatiepakket.naam, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
不起作用。我完全不明白为什么这不起作用。我在Ztc的Console.WriteLines信息后面的代码中制作了一个按钮。它说有一个 selectedEducatiePakket,但我的文本框没有显示它的任何信息。
有谁知道我做错了什么?
I have a WPF-page (ZorgTrajectPage1.xaml) with a code-behind ZorgTrajectPage1.xaml.cs with a DependencyProperty ZorgtrajectController called Ztc.
The dataContext is set in the Page_Loaded() of ZorgTrajectPage1.xaml:
Ztc = new ZorgTrajectController();
DataContext = Ztc;
It appears that I can access this ZorgTrajectController via binding to look up a integer in this Controller (patientID-variable):
<TextBox Name="textBox1" Text="{Binding Path=PatientID, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
This works perfect. But I also have a comboBox that selects an educatiePakket. When this selection is made, the selectedEducatiePakket is looked up via a LINQ-query and put in a variable in a ZorgTrajectController. This SelectedEducatiePakket is a new instance of a LINQ-class (that has a String-property with it's name, called naam). I used the following method to fill it up:
SelectedEducatiepakket = SelectedEducatiePakketByID(5);
public educatiepakket SelectedEducatiePakketByID(int id)
{
educatiepakket ep = (from o in db.educatiepakkets
where o.educatiepakket_id == id
select o).Single() as educatiepakket;
return ep;
}
Tthe following:
<TextBox Name="EPNaamTxtbx" Text="{Binding Path=SelectedEducatiepakket.naam, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
doesn't work. I totally don't understand why this isn't working. I've made a button which in the code-behind Console.WriteLines information of the Ztc. It says there is a selectedEducatiePakket, but my textbox isn't showing any information of it.
Does anyone know what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保
ZorgTrajectController
实现INotifyPropertyChanged
并在SelectedEducatiePakket
属性的 setter 中触发PropertyChanged
事件。像这样的事情:如果这没有帮助,请在调试器下运行您的项目,并在应该设置属性
SelectedEducatiePakket
后在 Visual Studio 的输出窗口中查看。可能有一些有关绑定错误的信息。Make sure that
ZorgTrajectController
implementsINotifyPropertyChanged
and fires thePropertyChanged
event in the setter of theSelectedEducatiePakket
property. Something like this:If that doesn't help, run your project under debugger and after the property
SelectedEducatiePakket
is supposed to be set look in the Visual Studio's Output window. There might be some information about binding errors.我看到一个错字。您的绑定适用于
SelectedEducatiepakket.naam
,但您说过,该属性名为SelectedEducatiePakket
。区别:“p”<-> “小包”中的“P”。I see a typo. Your Binding is for
SelectedEducatiepakket.naam
but you said, the property is namedSelectedEducatiePakket
. Difference: "p" <-> "P" in "Pakket".