本文介绍了如何只读WPF控件属性(如ActualWidth的)绑定所以它的价值是在视图模型访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,这样的值在视图模型可用来控制我的看法模型的只读属性绑定。

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?

更新:
我的问题实际上是这其中的一个副本:

推荐答案

检查这些链接

这篇关于如何只读WPF控件属性(如ActualWidth的)绑定所以它的价值是在视图模型访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:36