单个元素的多个DataContexts [英] Multiple DataContexts for a Single Element
问题描述
您好
我在DatagridTemplate列中有一个复选框。 datagrid绑定到一个集合。
I have a checkbox inside a DatagridTemplatecolumn. The datagrid is bound to a collection.
复选框,checked状态来自集合(datacontext1),而其可见性来自datacontext / viewmodel(datacontext2)。
The checkbox, checked state comes from the collection(datacontext1) whereas its visibility comes from the datacontext/viewmodel (datacontext2).
这里的最佳做法是什么?
What is the best practice here?
< CheckBox x:Name =" chkSelectPrimary" VerticalAlignment = QUOT;中心" IsChecked =" {Binding IsPrimary,Mode = TwoWay}"
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Visibility =" {Binding ColumnVisibility
<?PageDataContext?> }"的HorizontalAlignment = QUOT;中心" HorizontalContentAlignment =" Center" />
<CheckBox x:Name="chkSelectPrimary" VerticalAlignment="Center" IsChecked="{Binding IsPrimary, Mode=TwoWay}"
Visibility="{Binding ColumnVisibility
<?PageDataContext?>}" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
推荐答案
您可以指定您的来源在XAML中绑定。默认情况下,绑定的Source属性是继承的,但您可以将其覆盖为几乎可以访问的任何内容。
You can specify the source of your binding in XAML. By default, the Source property on a binding is inherited, but you can override it to be pretty much whatever you can access.
http://msdn.microsoft.com/en-us/library/system.windows.data.binding。 source.aspx
例如,您的网址可能如下所示:
As an example, yours might look like this:
<CheckBox x:Name="chkSelectPrimary" VerticalAlignment="Center" IsChecked="{Binding Property1, Mode=TwoWay}"
Visibility="{Binding Property2, Source={StaticResource ViewModel2}}" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
这篇关于单个元素的多个DataContexts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!