Getting a reference to the ViewModel from an IValueConverter

只愿长相守 提交于 2019-12-05 16:12:30

I would suggest adding a new property to the ViewModel that concatenates or combines the other properties in the ViewModel. This eliminates the need for an IValueConverter all together.

Check out this discussion on IValueConverters when using MVVM.

Whenever I have the rare scenario when I need to use multiple properties from a ViewModel in a converter, I just pass the entire view model as the input, ala;

<Textbox Text="{Binding, Converter={StaticResource WholeObjectConverter}"/>

As far as getting a reference to the window / page / view that invoked the conversion is not a good idea as this certainly breaks the MVVM pattern. The idea is that your views, and data are separate entities, and by design should have no knowledge of each other.

If you wanted to really hack it though, you could probably get clever and pass the references that you want (either the ViewModel or the View) as the ConverterParameter part of the binding .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!