基于对象名称的对象属性数据绑定到集合项
是否可以实现以下 WPF (Silverlight) 数据绑定方案?
页面上有许多 CustomControl:
<Grid x:Name="grid1">
...
<My:CustCntr x:Name="name1" Property1="{Binding Property1}" />
<My:CustCntr x:Name="name2" Property1="{Binding Property1}" />
<My:CustCntr x:Name="name3" Property1="{Binding Property1}" />
...
</Grid>
Grid 的 DataContext
是一个 ObservableCollection:
grid1.DataContext = myCollection;
...
ObservableCollection<MyEntity> myCollection= new ObservableCollection<MyEntity>();
...
MyEntity
类具有属性 Name
和 Property1
。
MyEntity me1 = new MyEntity { Name = "name1", Property1 = "5" };
MyEntity me2 = new MyEntity { Name = "name2", Property1 = "6" };
MyEntity me3 = new MyEntity { Name = "name3", Property1 = "7" };
...
myCollection.Add(me1);
myCollection.Add(me2);
myCollection.Add(me3);
...
我可以为每个 CustomControl 中的 Property1
建立数据绑定到 myCollection 的相应项目,其中 CustomControl 的 Name
等于 Name
的值code> 集合项的字段?
Is it possible to implement the following WPF (Silverlight) databinding scenario?
There are a number of CustomControls on a page:
<Grid x:Name="grid1">
...
<My:CustCntr x:Name="name1" Property1="{Binding Property1}" />
<My:CustCntr x:Name="name2" Property1="{Binding Property1}" />
<My:CustCntr x:Name="name3" Property1="{Binding Property1}" />
...
</Grid>
The Grid's DataContext
is an ObservableCollection:
grid1.DataContext = myCollection;
...
ObservableCollection<MyEntity> myCollection= new ObservableCollection<MyEntity>();
...
The MyEntity
class has properties Name
and Property1
.
MyEntity me1 = new MyEntity { Name = "name1", Property1 = "5" };
MyEntity me2 = new MyEntity { Name = "name2", Property1 = "6" };
MyEntity me3 = new MyEntity { Name = "name3", Property1 = "7" };
...
myCollection.Add(me1);
myCollection.Add(me2);
myCollection.Add(me3);
...
Can I establish databinding for Property1
in each of my CustomControls to a corresponding item of the myCollection where Name
of the CustomControl equals the value of Name
field of the collection item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,如果您有一个要在 UI 上显示的 Collection,您要做的就是使用
ItemsControl
、ListBox
等,并将 ItemsSource 设置为 Collection 。示例现在,每个
CustCntr
的 DataContext 将是MyEntity
的一个实例,并且绑定将在CustCntr.Property1
和MyEntity 之间设置。 Property1
也就是说,我不确定您当前实现的原因,因此如果您想根据名称创建绑定,我认为您必须求助于
Xaml
后面的代码< strong>代码隐藏
更新
每次在代码中修改集合时,请调用此方法 SetBindings。另外,请使用网格的 Loaded 事件来设置首次加载时的所有绑定。
Normally what you do in a situation where you have a Collection that you want to display on the UI is to use an
ItemsControl
,ListBox
etc. and set the ItemsSource to the Collection. ExampleNow the DataContext for each
CustCntr
will be an instance ofMyEntity
and the Binding will be set betweenCustCntr.Property1
andMyEntity.Property1
That said, I'm not sure of the reasons for your current implementation so if you want to create Bindings based on Name I think you'll have to resort to code behind
Xaml
Code behind
Update
Call this method, SetBindings, everytime you've modified the collection in code. Also, use the Loaded event for the Grid instead to set all bindings when it's first Loaded.
不,那是不可能的。
但是,您可以使用 ItemsControl< /a> 对象(有多个继承自它的控件),但那时设置设计可能会更困难。对于各个控件而言不是,但对于每个 CustCntr 的放置而言。
No, that would not be posible.
You could however use an ItemsControl object (there are multple controls that inherit from it), but it might be harder to set the design up then. No for the individual controls, but for the placement of each CustCntr.