WPF-绑定到集合中的特定元素 [英] WPF - binding to a specific element in a collection

查看:1012
本文介绍了WPF-绑定到集合中的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家们;)

在我的WPF应用程序中,我有一个名为 Employee 的类,其中包含2个字段: FirstName LastName .
在我的视图模型中,我有两个可绑定的属性:

Hi dear experts ;)

In my WPF app, I have a class called Employee with 2 fields: FirstName, LastName.
In my view model, I have two bindable properties:

ObservableCollection<Employee> Employees;<br />
int CurrentIndex;



我想将我的视图上的TextBlock绑定到当前雇员的FirstName字段;我已经测试了这些语法,但不幸的是没有一个起作用::omg​​:

a)<TextBlock Text="{Binding Employees[CurrentIndex].FirstName}"/>
b)<TextBlock Text="{Binding Employees[{Binding CurrentIndex}].FirstName}"/>

你能给我解决办法吗?
我不确定,但我认为可以使用CollectionViewSource.但实际上我不想使用它:laugh:

提前谢谢大家,
干杯,
Siavash



I want to bind a TextBlock on my view to the FirstName field of current employee; I''ve tested these syntaxs, but unfortunately none worked: :omg:

a) <TextBlock Text="{Binding Employees[CurrentIndex].FirstName}"/>
b) <TextBlock Text="{Binding Employees[{Binding CurrentIndex}].FirstName}"/>

Can you give me any solution?
I''m not sure but I think it''s possible to use CollectionViewSource; but actually I prefer not to use it :laugh:

Thank you all in advance,
Cheers,
Siavash

推荐答案

将新属性添加到名为CurrentEmployee 的视图模型中会更加容易,该属性将返回Employees[CurrentIndex].
It would be far more easier to add a new property to your view-model called CurrentEmployee which returns Employees[CurrentIndex].


感谢Nishant :)
你说得对;
我不想为此保留额外的参考;但似乎是不可避免的.

干杯,
Siavash
Thanks Nishant :)
You''re right;
I didn''t want to maintain an extra reference for that; but it seems it''s unavoidable.

Cheers,
Siavash


这篇关于WPF-绑定到集合中的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆