gpt4 book ai didi

c# - 组合框绑定(bind)时出现 NullReferenceException

转载 作者:太空狗 更新时间:2023-10-30 01:35:33 25 4
gpt4 key购买 nike

<分区>

我有一个保管箱:

<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}"
/>

使用此绑定(bind):

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 中读取数据更改为绑定(bind),但我对此遇到了问题。应该更改什么以修复它?

@更新正如我刚刚发现的:问题出在处理绑定(bind)期间。 Wpf 正在按此顺序处理绑定(bind) ->

  1. 固定驱动器
  2. 选择已更改
  3. DriveIsEnabled
  4. DriveSelected

并且处理 DriveSelected 正在触发值为 null 的“DriveSelection_SelectionChanged”。这会导致问题。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com