Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框绑定时出现NullReferenceException_C#_Wpf_Xaml - Fatal编程技术网

C# 组合框绑定时出现NullReferenceException

C# 组合框绑定时出现NullReferenceException,c#,wpf,xaml,C#,Wpf,Xaml,我有一个投递箱: <ComboBox Height="23" Name="DriveSelection" Width="120" ItemsSource="{Binding Path=FixedDrives}" DisplayMemberPath="Name" SelectedItem="{Binding Pat

我有一个投递箱:

<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}"
                              />
最后一行:
this.driveSelection=新的DriveInfo(this.root)我在此行中获取空引用异常:

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

  • 固定驱动器
  • 选择改变了
  • 驾驶执照
  • 驾驶选择

  • 并且处理
    DriveSelected
    正在触发'DriveSelection\u SelectionChanged',value=null。这导致了问题。

    看起来可能是DriveIsEnabled setter(不包括在代码中)正在调用updatePath()。 通过将构造函数更改为以下值,应确保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
        }
    

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

    this.driveSelection = new DriveInfo(this.root);
    
    不是
    FixedDevices
    集合的一部分。这导致
    null
    通过WPF绑定传递给您的属性

    在那之后支票

    if (value == this.driveSelection)
    
    在属性
    DriveSelection\u selection changed
    中,由于您已将
    新的DriveInfo(this.root)
    分配给变量
    DriveSelection
    ,因此会导致
    false


    检查失败导致driveSelection设置为null,然后在
    UpdatePath Manager()中抛出
    NullReferenceException

    Stacktrace或它不太可能
    driveSelection
    null
    。因此,请使用调试器查看您是否真的达到了您认为已将其设置为某个值的程度。尝试移动行this.driveSelection=new DriveInfo(this.root);到构造函数的开头,就在this.root=Path.GetPathRoot…
    UpdatePathManager
    DriveSelection\u SelectionChanged
    调用之后,它包含在我的代码段中。无论何时调用,绑定都不会检查此.DriveSelection是否为null,这意味着您需要确保它已设置在触发绑定之前,要执行此操作,只需在构造函数中创建DriveInfo实例,并可能在其上实现INotifyPropertyChange(如果您计划在绑定数据时更改其属性)。我用一些新信息更新了我的问题
    this.driveSelection = new DriveInfo(this.root);
    
    if (value == this.driveSelection)