Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
Wpf 组合框上的Selecteditem空引用异常_Wpf_Binding_Mvvm_Selecteditem_Selectionchanged - Fatal编程技术网

Wpf 组合框上的Selecteditem空引用异常

Wpf 组合框上的Selecteditem空引用异常,wpf,binding,mvvm,selecteditem,selectionchanged,Wpf,Binding,Mvvm,Selecteditem,Selectionchanged,我想使用SelectedItem从代码中将选择设置为组合框。 我只能使用SelectedValue使其工作。SelectedItem将在stacktrace的顶部引发一个空引用异常: 在AttachedCommandBehavior.CommandBehavior.Binding.Execute()处 XAML: <Window x:Class="MainWindowView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pres

我想使用SelectedItem从代码中将选择设置为组合框。 我只能使用SelectedValue使其工作。SelectedItem将在stacktrace的顶部引发一个空引用异常:

在AttachedCommandBehavior.CommandBehavior.Binding.Execute()处

XAML:

<Window x:Class="MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:acb="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
Title="Window1" Height="300" Width="300">
<StackPanel>
    <ComboBox Name="ComboItems1"
              DisplayMemberPath="Value" 
              SelectedValuePath="Key"
              ItemsSource="{Binding Items}" 
              SelectedValue="{Binding SelectedValue}" 
              acb:CommandBehavior.Event="SelectionChanged" 
              acb:CommandBehavior.Command="{Binding Path=SelectionChangedCommand}" 
              acb:CommandBehavior.CommandParameter="{Binding ElementName=ComboItems1, Path=SelectedItem}" />

    <ComboBox Name="ComboItems2"
              DisplayMemberPath="Value" 
              SelectedValuePath="Key"
              ItemsSource="{Binding Items}" 
              SelectedItem="{Binding SelectedItem}" 
              acb:CommandBehavior.Event="SelectionChanged" 
              acb:CommandBehavior.Command="{Binding Path=SelectionChangedCommand}" 
              acb:CommandBehavior.CommandParameter="{Binding ElementName=ComboItems2, Path=SelectedItem}"/>
</StackPanel>
公共类MainWindowViewModel

Private _mainWindowView As MainWindowView

Public Property Items As New List(Of KeyValuePair(Of Integer, String))
Public Property SelectedItem As Nullable(Of KeyValuePair(Of Integer, String))
Public Property SelectedValue As Nullable(Of Integer)
Public Property SelectionChangedCommand As ICommand

Public Sub New()

    Items.Add(New KeyValuePair(Of Integer, String)(1, "first item"))
    Items.Add(New KeyValuePair(Of Integer, String)(2, "second item"))
    Items.Add(New KeyValuePair(Of Integer, String)(3, "third item"))

    Dim simpleCommand As SimpleCommand = New SimpleCommand()
    simpleCommand.ExecuteDelegate = Sub(selectedItem As Object)
                                        HandleSelectionChanged(selectedItem)
                                    End Sub
    SelectionChangedCommand = simpleCommand

    SelectedValue = 1
    'SelectedItem = Items(1) 'uncomment this to raise the null ref exception

End Sub

Private Sub HandleSelectionChanged(ByRef selectedItem As Object)
    If selectedItem IsNot Nothing Then
        'Do something
    End If
End Sub
末级

为什么selecteditem不起作用

更新:

尼古拉:你的眼睛很敏锐。这是由于最后一分钟的复制粘贴工作

Blindmeis:当然,这是一个更大的程序的抽象,我需要selectionchanged事件来执行一些操作。这些命令绑定必须保留(尽管可能需要一些修复)

问候,

米歇尔

您没有名为ComboItems的元素,您有ComboItems1和ComboItems2。我认为这就是问题所在


您没有名为ComboItems的元素,您有ComboItems1和ComboItems2。我认为这就是问题所在。

为什么要使用这些命令绑定

    <ComboBox 
          DisplayMemberPath="Value" 
          SelectedValuePath="Key"
          ItemsSource="{Binding Items}" 
          SelectedItem="{Binding SelectedItem}" />
这很有效

编辑:

视图模型

    //this select the "third item" in your combobox
    SelectedItem = Items[2];/dont know the vb indexer stuff ;)
     public KeyValuePair<int, string> SelectedItem
     {
        get{return this._selectedItem;}
        set{

           if(this._selectedItem==value)
               return;//no selection change

           //if you got here then there was a selection change
           this._selectedItem=value;
           this.OnPropertyChanged("SelectedItem");
           //do all action you want here
           //and you do not need selection changed event commmandbinding stuff

         }
     }      
public KeyValuePair SelectedItem
{
获取{返回此。\u selectedItem;}
设置{
如果(此._selectedItem==值)
return;//没有选择更改
//如果你到了这里,你的选择就改变了
这是。\ u selectedItem=value;
此.OnPropertyChanged(“SelectedItem”);
//你想在这里采取什么行动
//而且您不需要更改事件绑定的内容
}
}      

为什么要使用这些命令绑定

    <ComboBox 
          DisplayMemberPath="Value" 
          SelectedValuePath="Key"
          ItemsSource="{Binding Items}" 
          SelectedItem="{Binding SelectedItem}" />
这很有效

编辑:

视图模型

    //this select the "third item" in your combobox
    SelectedItem = Items[2];/dont know the vb indexer stuff ;)
     public KeyValuePair<int, string> SelectedItem
     {
        get{return this._selectedItem;}
        set{

           if(this._selectedItem==value)
               return;//no selection change

           //if you got here then there was a selection change
           this._selectedItem=value;
           this.OnPropertyChanged("SelectedItem");
           //do all action you want here
           //and you do not need selection changed event commmandbinding stuff

         }
     }      
public KeyValuePair SelectedItem
{
获取{返回此。\u selectedItem;}
设置{
如果(此._selectedItem==值)
return;//没有选择更改
//如果你到了这里,你的选择就改变了
这是。\ u selectedItem=value;
此.OnPropertyChanged(“SelectedItem”);
//你想在这里采取什么行动
//而且您不需要更改事件绑定的内容
}
}      

尝试将注释掉的行更改为
SelectedValue=2
我感觉它是您的事件绑定在selectionChanged上,当您的SelectedItem更改选择更改时,您可以在不发出命令的情况下在vm中处理吗?或者我错过了什么?ExitMusic:我希望它与SelectedItem一起工作,我正在演示SelectedValue可以工作,但SelectedItem不能。SelectedItem用于ComboItems2。Blindmeis,正确:commandbinding让我直接在viewmodel中处理selectionchanged事件,而不必在视图中执行管道操作。尝试将注释掉的行更改为
SelectedValue=2
当您的SelectedItem更改选择时,我感觉它是您在selectionchanged上的事件绑定,因此您可以在没有命令的情况下处理虚拟机?或者我错过了什么?ExitMusic:我希望它与SelectedItem一起工作,我正在演示SelectedValue可以工作,但SelectedItem不能。SelectedItem用于ComboItems2。Blindmeis,更正:commandbinding让我直接在viewmodel中处理selectionchanged事件,而不必在视图中执行管道。我没有考虑过这一点,这当然是可以接受的。我将从二传手那里提出一个事件。如果有人知道初始问题发生的原因,请告诉我:)。您已经在setter中引发了一个事件:)INotifyPropertyChanged!我还没想过,那当然是可以接受的。我将从二传手那里提出一个事件。如果有人知道初始问题发生的原因,请告诉我:)。您已经在setter中引发了一个事件:)INotifyPropertyChanged!