如何只读WPF控件属性(如ActualWidth的)绑定所以它的价值是在视图模型访问? [英] How to bind a read-only WPF control property (eg ActualWidth) so its value is accessible in the view model?
问题描述
我想,这样的值在视图模型可用来控制我的看法模型的只读属性绑定。
I want to bind a read-only property of a control to my view model so that the value is available in the view model.
什么是最好的方式这样做呢?
What is the best way of doing this?
例如我想 ActualWidth的
绑定到我的视图模型的属性。宽度值是通过使用WPF的布局逻辑,所以我不能产生在我看来,这种模式的价值和它推到控件属性生成的,通常的做法是这样的。相反,我需要有WPF产生的价值,并将其推到视图模型。
For example I'd like to bind ActualWidth
to a property in my view model. The width value is generated by WPF using its layout logic so I can't generate this value in my view model and push it to the control property, as would normally be the case. Instead I need to have WPF generate the value and push it to the view model.
我只想用模式= OneWayToSource
,但这并不为只读属性工作:
I would just use Mode=OneWayToSource
, but this doesn't work for read-only properties:
<Border
...
ActualWidth="{Binding MyDataModelWidth, Mode=OneWayToSource}"
>
... child controls ...
</Border>
我目前在做它的方式是处理时,SizeChanged
的边框和隐藏代码插入值到视图模式,但这并不手感相当不错。
The way I am doing it currently is to handle SizeChanged
for the border and the code-behind plugs the value into the view model, but this doesn't feel quite right.
有没有人已经解决了这个问题?
Has anyone already solved this problem?
更新:
我的问题实际上是这其中的一个副本:
的推只读GUI性能回视图模型
推荐答案
检查这些链接
的 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0ed9ec9d-00b3-44ee-afa6-43396daf88dc/
这篇关于如何只读WPF控件属性(如ActualWidth的)绑定所以它的价值是在视图模型访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!