Combobox绑定时出现NullReferenceException

时间:2014-08-28 11:17:53

标签: c# wpf xaml

我有一个保管箱:

<ComboBox Height="23" Name="DriveSelection" Width="120"
                              ItemsSource="{Binding Path=FixedDrives}"
                              DisplayMemberPath="Name"
                              SelectedItem="{Binding Path=DriveSelection_SelectionChanged}"
                              IsSynchronizedWithCurrentItem="True"
                              IsEnabled="{Binding DriveIsEnabled}"
                              SelectedValue="{Binding DriveSelected}"
                              />

使用此绑定:

private ObservableCollection<DriveInfo> fixedDrives;
public ObservableCollection<DriveInfo> FixedDrives
{
    get
    {
        if (this.fixedDrives != null)
            return this.fixedDrives;
        this.fixedDrives = new ObservableCollection<DriveInfo>(Enumerable.Where<DriveInfo>((IEnumerable<DriveInfo>)DriveInfo.GetDrives(), (Func<DriveInfo, bool>)(driveInfo => driveInfo.DriveType == DriveType.Fixed)));
        return this.fixedDrives;
    }
}


public DriveInfo DriveSelection_SelectionChanged
{
    get
    {
        return this.driveSelection;
    }
    set
    {
        if (value == this.driveSelection)
            return;
        this.driveSelection = value;
        UpdatePathManager();
        this.OnPropertyChanged("DriveSelection_SelectionChanged");
    }
}
public object DriveSelected
{
    get
    {
        return _driveSelected;
    }
    set
    {
        _driveSelected = value;
        RaisePropertyChanged("DriveSelected");
    }
}

并在进行页面初始化时:

public PathSelectionPageViewModel(PathSelectionPage _page)
{
    this.page = _page;
    this.root = Path.GetPathRoot(App.Instance.PathManager.InstallRoot).ToUpperInvariant();
    this.DriveSelected = (object)this.root;
    //this.page.DriveSelection.SelectedValue = (object)this.root;
    this.DriveIsEnabled = true
    //this.page.DriveSelection.IsEnabled = true
    this.driveSelection = new DriveInfo(this.root);
}

在最后一行: this.driveSelection = new DriveInfo(this.root);我在这一行中提到空引用异常:

private void UpdatePathManager()
{
    string newRoot = this.driveSelection.ToString(); <--- this line
    //string newRoot = this.page.DriveSelection.SelectedValue.ToString();
}

正如您所看到的,我只是试图将读取数据直接从View更改为绑定,但我遇到了问题。 为了解决这个问题需要改变什么?

@Update 我刚刚发现: 问题在于处理绑定期间。 Wpf按此顺序处理绑定 - &gt;

  1. FixedDrives
  2. 选择已更改
  3. DriveIsEnabled
  4. DriveSelected
  5. 并且处理DriveSelected正在触发带有value = null的`DriveSelection_SelectionChanged。这就是导致问题。

2 个答案:

答案 0 :(得分:2)

这里的真正问题是您使用此代码分配的new DriveInfo(this.root)

this.driveSelection = new DriveInfo(this.root);

不属于您的FixedDevices集合。这导致WPF-Binding将null传递给您的属性。

之后检查

if (value == this.driveSelection)
属性DriveSelection_SelectionChanged中的

会产生false,因为您已将new DriveInfo(this.root)分配给变量driveSelection

检查失败导致driveSelection设置为null,然后在NullReferenceException

中抛出UpdatePathManager()

答案 1 :(得分:1)

看起来可能是DriveIsEnabled setter(未包含在您的代码中)正在调用UpdatePathManger()。 您应该通过将构造函数更改为:

来确保this.driveSelection永远不为null
    public PathSelectionPageViewModel(PathSelectionPage _page)
    {
        this.page = _page;
        this.root = Path.GetPathRoot(App.Instance.PathManager.InstallRoot).ToUpperInvariant();
        this.driveSelection = new DriveInfo(this.root);
        this.DriveSelected = (object)this.root;
        //this.page.DriveSelection.SelectedValue = (object)this.root;
        this.DriveIsEnabled = true
        //this.page.DriveSelection.IsEnabled = true
    }