ComboBox绑定时出现了空引用异常

4

我有一个Dropbox:

<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();
}

如您所看,我只是试图将来自视图的读取数据直接转换为绑定,但我遇到了问题。 应该改变什么来解决它?
@更新 正如我刚刚发现的: 问题在于处理绑定时。WPF按照以下顺序处理绑定-> 1. 固定驱动器 2. 选择更改 3. 驱动器启用 4. 已选择驱动器
处理`DriveSelected`会触发带有值=null的“DriveSelection_SelectionChanged”,这就导致了问题。

3
堆栈跟踪或者它没有发生。 - leppie
1
显然,driveSelectionnull。因此,请使用调试器查看您是否实际上到达了您认为已将其设置为某个值的点。 - Dirk
尝试将此行代码 this.driveSelection = new DriveInfo(this.root); 移动到构造函数的开头,就在 this.root = Path.GetPathRoot 之后... - too
2个回答

2
这里的真正问题是,你使用这段代码分配的new DriveInfo(this.root)
this.driveSelection = new DriveInfo(this.root);

该属性不属于您的FixedDevices集合。这导致WPF绑定将null传递给您的属性。

之后进行检查

if (value == this.driveSelection)

在属性 DriveSelection_SelectionChanged 中,由于您将 new DriveInfo(this.root) 分配给变量 driveSelection,导致结果为false
这个失败的检查导致 driveSelection 被设置为 null,然后在 UpdatePathManager() 中抛出 NullReferenceException

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
    }

UpdatePathManager 在代码片段中被 DriveSelection_SelectionChanged 调用。 - szpic
无论何时调用,您的绑定都不会检查this.driveSelection是否为null,这意味着您需要确保在触发绑定之前设置它,并在构造函数中创建DriveInfo实例,如果您计划在数据绑定时更改其属性,则可能要实现INotifyPropertyChange。 - too
我更新了我的问题并附上了一些新信息。 - szpic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接