Value of text box disapears - binding viewmodel to a tab (content control)

不打扰是莪最后的温柔 提交于 2020-02-02 06:29:09

问题


Based on the MVVM example by Josh Smith, I have implemented the multi tab option which binds to a different tab to a different view model using a simple datatemplate that binds a viewmodel to a view.

 <DataTemplate  DataType="{x:Type fixtureVM:SearchViewModel}">
    <SearchVw:SearchView/>
</DataTemplate>

The issue that I'm having, is when I switch tabs and then switch back again, the value in the textbox disappears. When I bind the Text in the textbox to a value in the ViewModel it does not disappear. This is fine, and I can overcome this but I am having another issue for example with the position of the scroll bar in a grid disappearing once the tab has lost focus.

Why is the value disappearing? I'm assuming it is a WPF sub system task that cleans up resources!? how can I avoid this? I also feel it might be slowing down my app.


回答1:


Read my post here about why this is happening. Basically, because you are connecting the View and ViewModel in a DataTemplate, the tab re-creates the view every time it receives the focus. And yes, this eats up more resources. You can fix this by hooking up the view and ViewModel in a different way (i.e. Catelog method, WAF, or using a different type of items control may do the trick as well...)




回答2:


Here is a solution that creates a subclass of the TabControl.

http://eric.burke.name/dotnetmania/2009/04/26/22.09.28



来源:https://stackoverflow.com/questions/2835430/value-of-text-box-disapears-binding-viewmodel-to-a-tab-content-control

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