ViewModel 如何在需要时从视图请求数据?
我的视图上有一个计算属性,我需要将其绑定到我的 ViewModel。我正在使用 WPF,似乎无法创建可自计算的可绑定属性(依赖属性)。我不想在视图状态发生变化时执行计算,因为它们非常耗时。我想在 ViewModel 需要结果时(即关闭时)进行计算。
I have a calculated property on my View that I need to bind to my ViewModel. I'm using WPF and it seems that there is no way to make a bindable property (Dependency Property) that is self calculating. I don't want to perform the calculations whenever the View's state changes because they are time intensive. I want to do the calculations whenever the ViewModel needs the result, i.e. when it closes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您上面的评论,我将使用
Converter
您的 ViewModel 将包含加密数据,并且与视图的绑定使用 Converter 将其转换为可读的内容。当需要将数据保存回 ViewModel 时,请使用转换器的
ConvertBack
方法再次加密数据。如果加密代码需要一段时间,请设置
UpdateSourceTrigger=Explicit
并在单击“保存”按钮时手动触发源更新。Based on your comment above, I'd use a
Converter
Your ViewModel would contain the encrypted data, and the binding to the View uses a Converter which converts it into something readable. When it's time to save the data back to the ViewModel, use the
ConvertBack
method of the converter to encrypt the data again.If the Encryption code takes a while, set your
UpdateSourceTrigger=Explicit
and manually trigger the source update when the Save button is clicked.这是我的解决方案。它的工作方式与
ICommand
相同,但视图提供委托 (CalculationDelegate
),并且视图模型调用CanExecute
和Execute.它不是纯粹的 MVVM,但它可以工作。
我已将 Rachel 的答案标记为正确,只是因为我在这里所做的不是纯粹的 MVVM。
This is my solution. It works the same way as
ICommand
but the view provides the delegate (CalculationDelegate
) and the view model callsCanExecute
andExecute
. Its not pure MVVM but it works.I have marked Rachel's answer as correct, simply because what I am doing here is not pure MVVM.