我有一个与WPF和MVVM一起使用的ViewModel类:
public class ViewModel {
/* Other members here... */
public ReadOnlyObservableCollection<BackplaneViewModel> Backplanes {
get { return _Backplanes; }
}
public BackplaneViewModel CurrentBackplane {
get {
var cb = _CurrentBackplane ?? (_CurrentBackplane = Backplanes.First());
return cb;
}
set {
if (_CurrentBackplane == value) return;
_CurrentBackplane = value;
RaisePropertyChanged("CurrentBackplane");
}
}
}
_Backplanes
集合已在构造函数中创建并填充,并且永不更改。
我有一个使用此ViewModel
的实例作为其DataContext
的控件。用户可以通过CurrentBackplane
选择ComboBox
:
<ComboBox ItemsSource="{Binding Backplanes}"
SelectedItem="{Binding CurrentBackplane}"
DisplayMemberPath="BackplaneIndex" />
CurrentBackplane
也可以在代码中更改。
我在get
的CurrentBackplane
上设置了一个断点。 cb
变量为not null。但是,在WPF
请求其值之后,我立即在Output窗口中获得以下内容:
System.Windows.Data Information: 40 : BindingExpression path error: 'BackplaneIndex' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=BackplaneIndex; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 19 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=BackplaneIndex; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=BackplaneIndex; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=BackplaneIndex; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
为什么WPF
告诉我数据项为空?
我不确定我在做什么错。该程序实际上运行良好,但我试图找出可能与该问题有关的内存泄漏。
_CurrentBackplane = Backplanes.First();
和
public BackplaneViewModel CurrentBackplane {
get {
return _CurrentBackplane;
}
set { _CurrentBackplane = value; }
}
set { _CurrentBackplane = value; OnPropertyChanged("CurrentBackplane "); }
并且您应该将SelectedItem的Mode设置为TwoWay
SelectedItem="{Binding CurrentBackplane, Mode=TwoWay}"
顺便说一句,您只获得绑定信息,而不是绑定错误
System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;
这可能会解决问题。
var resolvedSource = bindingExpression.ResolvedSource;
if (resolvedSource == null)
{
textBox.DataContextChanged += dataContextChanged;
static void dataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var frameworkElement = (FrameworkElement)sender;
var be = frameworkElement.GetBindingExpression(TextBox.TextProperty);
var newBe = BindingOperations.SetBinding(
(DependencyObject)sender, TextBox.TextProperty, be.ParentBinding);
Debug.Assert(newBe is BindingExpression);
Debug.Assert(newBe.DataItem != null);
//here newBe.DataItem should not be null
addValidationRules((BindingExpression)newBe);
frameworkElement.DataContextChanged -= dataContextChanged;
}
return;
}
addValidationRules(bindingExpression);