我有一個下拉框:的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正在處理的綁定的順序 - >
- FixedDrives
- 選擇改變
- DriveIsEnabled
- DriveSelected
和處理DriveSelected
與值= NULL燒製`DriveSelection_SelectionChanged」和這是造成問題的原因。
堆棧跟蹤或者它沒有發生 – leppie 2014-08-28 11:19:40
Apparantly'driveSelection'是'null'。因此,使用調試器來查看是否真的達到了您認爲將其設置爲某個值的地步。 – Dirk 2014-08-28 11:23:23
嘗試移動這一行this.driveSelection = new DriveInfo(this.root);到構造函數的開頭,在this.root = Path.GetPathRoot之後... – too 2014-08-28 11:24:05