[WPF表示数据项不为null时为null

问题描述 投票:2回答:5

我有一个与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也可以在代码中更改。

我在getCurrentBackplane上设置了一个断点。 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告诉我数据项为空?

我不确定我在做什么错。该程序实际上运行良好,但我试图找出可能与该问题有关的内存泄漏。

.net wpf xaml data-binding mvvm
5个回答
0
投票
_CurrentBackplane = Backplanes.First();

public BackplaneViewModel CurrentBackplane {  
    get {   
        return _CurrentBackplane;  
    }  
    set { _CurrentBackplane = value; }  
}  

0
投票
set { _CurrentBackplane = value; OnPropertyChanged("CurrentBackplane "); }

并且您应该将SelectedItem的Mode设置为TwoWay

SelectedItem="{Binding CurrentBackplane, Mode=TwoWay}"

顺便说一句,您只获得绑定信息,而不是绑定错误


0
投票
我的代码遇到类似的问题,很遗憾,我还没有弄清楚如何防止它,但是如果可以,我会及时通知你。

0
投票
System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;

这可能会解决问题。


0
投票
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);
© www.soinside.com 2019 - 2024. All rights reserved.