【发布时间】:2009-12-14 22:37:25
【问题描述】:
这几天我一直在绕圈子,我希望 WPF 专家能看到我哪里出错了。
我正在代码中设置 CurrentViewModel。我的 ListBox 的 Selected 项和我的 ContentControl 的内容正确绑定。但是,当通过 UI 更改列表框中的选定项目时,正在设置 CurrentViewModel,但没有更新内容控件。
我正在使用数据模板来映射我的视图和视图模型。
<DataTemplate DataType="{x:Type ViewModel:MyViewModel}">
<View:MyView />
</DataTemplate>
我有一个绑定到 ViewModel 的可观察集合的 ListBox。选定项绑定到当前视图模型。
<ListBox ItemsSource="{Binding MyViewModelCollection}" DisplayMemberPath="DisplayName" SelectedItem="{Binding CurrentViewModel, Mode=TwoWay}"/>
我还有一个也绑定到 CurrentView 模型的内容控件
<ContentControl Content="{Binding CurrentViewModel, Mode=TwoWay}"/>
这是它们都绑定到的属性
public MyViewModel CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel== value) return;
_currentViewModel= value;
OnPropertyChanged("CurrentViewModel");
}
}
为了清晰起见,我编辑了名称并删除了格式信息。
非常感谢任何帮助。
干杯,
丹尼尔
编辑:看到链接How can I debug WPF bindings?。我在内容绑定上设置了一个断点,它确实只在第一次设置绑定时被调用一次。
【问题讨论】: