WPF 与 LINQ 类的绑定问题

发布于 2024-10-19 19:30:28 字数 1364 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-10-26 19:30:28

确保 ZorgTrajectController 实现 INotifyPropertyChanged 并在 SelectedEducatiePakket 属性的 setter 中触发 PropertyChanged 事件。像这样的事情:

public educatiepakket SelectedEducatiePakket 
{
   get { return _selectedEducatiePakket; }
   set 
   {
      _selectedEducatiePakket = value;
      RaisePropertyChanged("SelectedEducatiePakket");
   }
}

如果这没有帮助,请在调试器下运行您的项目,并在应该设置属性 SelectedEducatiePakket 后在 Visual Studio 的输出窗口中查看。可能有一些有关绑定错误的信息。

Make sure that ZorgTrajectController implements INotifyPropertyChanged and fires the PropertyChanged event in the setter of the SelectedEducatiePakket property. Something like this:

public educatiepakket SelectedEducatiePakket 
{
   get { return _selectedEducatiePakket; }
   set 
   {
      _selectedEducatiePakket = value;
      RaisePropertyChanged("SelectedEducatiePakket");
   }
}

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.

み青杉依旧 2024-10-26 19:30:28

我看到一个错字。您的绑定适用于 SelectedEducatiepakket.naam,但您说过,该属性名为 SelectedEducatiePakket。区别:“p”<-> “小包”中的“P”。

I see a typo. Your Binding is for SelectedEducatiepakket.naam but you said, the property is named SelectedEducatiePakket. Difference: "p" <-> "P" in "Pakket".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文