2014-08-28 60 views
4

我有一個下拉框:的NullReferenceException而組合框結合

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

正如你所看到的,我只是試圖改變直接從視圖讀取數據到綁定,但我有這個問題。 爲了解決這個問題需要改變什麼?

@Update 正如我剛剛發現的: 問題在處理綁定期間。 WPF正在處理的綁定的順序 - >

  1. FixedDrives
  2. 選擇改變
  3. DriveIsEnabled
  4. DriveSelected

和處理DriveSelected與值= NULL燒製`DriveSelection_SelectionChanged」和這是造成問題的原因。

+3

堆棧跟蹤或者它沒有發生 – leppie 2014-08-28 11:19:40

+1

Apparantly'driveSelection'是'null'。因此,使用調試器來查看是否真的達到了您認爲將其設置爲某個值的地步。 – Dirk 2014-08-28 11:23:23

+0

嘗試移動這一行this.driveSelection = new DriveInfo(this.root);到構造函數的開頭,在this.root = Path.GetPathRoot之後... – too 2014-08-28 11:24:05

回答

2

真正的問題是,new DriveInfo(this.root)您使用此代碼

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

分配是不是你FixedDevices的一部分採集。這導致null被WPF綁定傳遞給你的屬性。

後,辦理入住手續

if (value == this.driveSelection) 

在屬性DriveSelection_SelectionChanged結果false,因爲你已經分配new DriveInfo(this.root)給變量driveSelection

發生故障的檢查導致該driveSelection被設置爲空,然後拋出NullReferenceExceptionUpdatePathManager()

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 
    } 
+0

'UpdatePathManager'由'DriveSelection_SelectionChanged'調用,它包含在我的代碼片段 – szpic 2014-08-28 11:28:45

+0

中。無論何時調用它,您的綁定都不會檢查this.driveSelection爲null,這意味着您需要確保在綁定之前設置它觸發並執行此操作只是在構造函數中創建DriveInfo實例,並且如果您打算在數據綁定時更改其屬性,則可能會在其上實現INotifyPropertyChange。 – too 2014-08-28 11:36:10

+0

我用一些新信息更新了我的問題 – szpic 2014-08-28 11:36:50