将数据上下文上的属性绑定到数据上下文中包含的另一个对象
考虑下面所示的场景
class MyViewModel
{
public bool IsSelected {get;set;}
}
class SomeClass
{
public bool IsSelected {get;set;}
public object Data {get;}
}
<DataTemplate x:Key="ItemTemplate>
<Image ... />
</DataTemplate>
<SomeControl ItemsSource={Binding MyViewModels}"
ItemTemplate={StaticResource ItemTemplate}" />
SomeControl
和 SomeClass
类是我无法修改的第三方类。在内部,SomeControl
创建 SomeClass
的实例,并将我的视图模型分配给它的 Data
属性。因此,我的 ItemTemplate 的数据上下文是 SomeClass
我想将 SomeClass
上的 IsSelected
属性绑定到 IsSelected
MyViewModel
的属性。我该怎么做?
Consider the scenario shown below
class MyViewModel
{
public bool IsSelected {get;set;}
}
class SomeClass
{
public bool IsSelected {get;set;}
public object Data {get;}
}
<DataTemplate x:Key="ItemTemplate>
<Image ... />
</DataTemplate>
<SomeControl ItemsSource={Binding MyViewModels}"
ItemTemplate={StaticResource ItemTemplate}" />
The classes SomeControl
and SomeClass
are third party classes which I cannot modify. Internally, SomeControl
creates instances of SomeClass
and assigns my view model to its Data
property. So, the data context of my ItemTemplate is SomeClass
I want to bind the IsSelected
property on SomeClass
to the IsSelected
property of MyViewModel
. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我会考虑放弃一个第三方控件,该控件的功能与
ItemsControl
的正常工作方式不一致。您的数据上下文应该是您的视图模型。时期。也就是说,如果项目容器是这样的
SomeClass
,您应该能够解决这个问题:First, I would consider ditching a third-party control that does something so at odds with how the
ItemsControl
normally works. Your data context should be your view model. Period.That said, you should be able to work around it if the item container is a
SomeClass
like this: