[英]WPF how to access other variable from same viewmodel?
To display items on Telerik:RadGridView
usually I use DataContext="{Binding [someViewModel]}"
and ItemSource="{Binding objectList, Mode=TwoWay}"
. 为了在Telerik:RadGridView
上显示项目,通常我使用DataContext="{Binding [someViewModel]}"
和ItemSource="{Binding objectList, Mode=TwoWay}"
。
and for my column I'll access the objectfield
. 对于我的专栏,我将访问objectfield
。 The overall picture will be something like below: 总体情况如下所示:
<telerik:RadGridView DataContext="{Binding [someViewModel]}"
ItemSource="{Binding objectList, Mode=TwoWay}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<CheckBox IsEnabled="{Binding enabledVar}"
IsChecked="{Binding isChecked, Mode=Twoway}"
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
Imagine there will be 10 items in objectList
. 想象在objectList
中将有10个项目。 Each of the item in objectList
will have a variable isChecked
which to manipulate the checkbox IsChecked
property. objectList
每个项目都会有一个isChecked
变量,该变量可操作复选框IsChecked
属性。
I have another variable in the same viewmodel named enabledVar
which to control the ten checkbox IsEnabled
property. 我在同一个视图模型中有另一个名为enabledVar
变量,用于控制十个复选框的IsEnabled
属性。 enabledVar
is not part of the objectList
but I couldn't get the value. enabledVar
不是的一部分objectList
,但我不能得到的价值。 May I know how to handle such case? 请问该如何处理?
Updates: 更新:
I've found some new direction but not sure if it helps. 我找到了一些新的方向,但不确定是否有帮助。
<CheckBox IsEnabled="{Binding enabledVar,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=telerik:RadGridView}}"
but then of course, still failed. 但是当然,仍然失败了。
Any helps would be very much appreciated. 任何帮助将不胜感激。
if using Ancestor you have to bind to DataContext 如果使用Ancestor,则必须绑定到DataContext
<CheckBox IsEnabled="{Binding Path=DataContext.enabledVar,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=telerik:RadGridView}}"
Another aproach could be to use element binding set name 另一个方法可能是使用元素绑定集名称
<telerik:RadGridView x:Name="myGrid" ...
And then in the celltemplate bind to it 然后在单元模板中绑定到它
<CheckBox IsEnabled="{Binding Path=DataContext.enabledVar, ElementName=myGrid}"
Hope this helps. 希望这可以帮助。
The code suggested in Update will not work because GridViewDataColumn is not part of the Visual tree, and hence can not access DataContext. Update中建议的代码将不起作用,因为GridViewDataColumn不属于Visual树,因此无法访问DataContext。
You will have to make use of proxy data binding, like suggested here . 你将不得不使用代理数据的结合,如建议在这里 。 You can get more examples by searching "wpf proxy databinding". 您可以通过搜索“ wpf代理数据绑定”来获取更多示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.