【发布时间】:2010-12-02 03:37:46
【问题描述】:
更新 2010 06 12
将精华提炼成更小的样品 - 可在此处获取解决方案 http://db.tt/v6r45a4
现在似乎是与选项卡控件有关的问题: 重新应用数据上下文时(单击按钮),似乎一切都按预期工作。绑定刷新,但活动选项卡上的所有绑定都被破坏。
您可以通过选择任何选项卡并单击按钮来验证这一点,您会看到该选项卡上的控件运行正常。添加了一堆日志语句,这就是我的朋友如何得出这个概要和他的修复[在选项卡控件上设置 DataContext="{Binding}" ]
但我们仍然不确定为什么会这样......
TabControl Data Context now set to ReproTabItemBug.MainViewModel
TabPage [LeftTabPage] Data Context now set to ReproTabItemBug.LeftViewModel
System.Windows.Data Error: 40 : BindingExpression path error: 'MiddleProp' property not found on 'object' ''MainViewModel' (HashCode=50608417)'. BindingExpression:Path=MiddleProp; DataItem='MainViewModel' (HashCode=50608417); target element is 'TextBox' (Name='MiddleTabTextbox'); target property is 'Text' (type 'String')
TabPage [MiddleTabPage] Data Context now set to ReproTabItemBug.MiddleViewModel
Middle tab textbox text changed to 634272638824920423
TabPage [RightTabPage] Data Context now set to ReproTabItemBug.RightViewModel
Middle tab textbox text changed to
上一篇文章 (免责声明:前面的帖子很长......拿你的爆米花。我花了一天的大部分时间在这上面......)
我的 ViewModel 由三个 POCO subViewModel 组成。每个 subVieModel 都具有绑定在上述 3 个部分中的属性。每个 subViewModel 都作为标准的 .net get-only 属性公开(无 INotifyPropertyChanged)
我的视图有 3 个部分。每个部分都有一个类似这样的 DataContext 设置器..
...
<TabItem x:Name="_tabPageForVM2" DataContext="{Binding PropReturningSubVM2}">
<!-- followed by UI Items that are data-bound to props inside this sub-viewmodel -->
</TabItem>
...
总结一下
<MainView> <!--DataContext set programmmatically to an Instance Of MainViewModel) -->
<Control DataContext="{Binding PropReturningSubVM1}" >.. Section1 .. </Control>
<Control DataContext="{Binding PropReturningSubVM2}" >.. Section2 .. </Control>
<Control DataContext="{Binding PropReturningSubVM3}" >.. Section3 .. </Control>
</MainView>
现在是令人费解的部分。 在正常启动时,我创建了 MainViewModel 的一个实例(将子视图模型传递到它的 ctor 中)。监视窗口中的属性证实了这一点。
Trace.WriteLine("Before setting datacontext");
mainView.DataContext = null;
mainView.DataContext = mainViewModel;
//mainView.Refresh();
Trace.WriteLine("After setting datacontext");
一切都很完美。现在由于我无法控制的原因,存在 UI 被关闭但视图仍驻留在内存中的情况。因此,为了在下次显示时将其清除,我创建了我的视图模型的新实例并重新应用数据上下文(通过调用与以前相同的初始化例程)
但是,当执行DataContext=mainViewModel 集时,我在输出窗口中看到一堆绑定错误。有趣的是,只有一个标签页(子视图模型)内的绑定被破坏了。其他 2 个子视图模型正常运行 - 没有绑定错误。
System.Windows.Data Error: 40 : BindingExpression path error: 'RphGaugeMaxScale' property not found on 'object' ''MainViewModel' (HashCode=38546056)'. BindingExpression:Path=RphGaugeMaxScale; DataItem='MainViewModel' (HashCode=38546056); target element is 'Gauge' (Name='GaugeControl'); target property is 'MaxValue' (type 'Double')
System.Windows.Data Error: 40 : BindingExpression path error: 'RphGaugeMaxScale' property not found on 'object' ''MainViewModel' (HashCode=38546056)'. BindingExpression:Path=RphGaugeMaxScale; DataItem='MainViewModel' (HashCode=38546056); target element is 'Gauge' (Name='GaugeControl'); target property is 'MajorTickCount' (type 'Int32')
...
属性存在于 SubViewModel2 上,但查找使用的是 MainViewModel。
接下来我在 MainView 上添加了一个 Refresh 方法(在重新应用 DataContext 后调用)来执行此操作
_tabPageForVM2.DataContext = null;
_tabPageForVM2.DataContext = mainVM.PropReturningSubVM2;
这解决了问题。
让我困惑的是
- subVM2 有什么特别之处?如果它在第一次分配 DataContext 时有效,为什么第二次会中断?
- 我将 tabPage 的 header 设置为 {Binding} 以检查它绑定的内容,它显示“FullTypeNameOfSubVM2”,这表明正在设置标签页的 DataContext 属性。那么为什么绑定被破坏了呢?
【问题讨论】:
标签: wpf data-binding