wpf - 组合框空引用异常上的 Selecteditem

标签 wpf binding mvvm selecteditem selectionchanged

我想使用 SelectedItem 将选择设置为代码中的组合框。
我只能通过使用 SelectedValue 让它工作。 SelectedItem 将在堆栈跟踪的顶部抛出一个空引用异常:

在 AttachedCommandBehavior.CommandBehaviorBinding.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>

编码:
Imports AttachedCommandBehavior

公共(public)类 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

结束类

为什么选择项不起作用?

更新:

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

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

问候,

米歇尔

最佳答案

为什么你有这些命令绑定(bind)?

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

View 模型
    //this select the "third item" in your combobox
    SelectedItem = Items[2];/dont know the vb indexer stuff ;)

这行得通。

编辑:

View 模型
     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

         }
     }      

关于wpf - 组合框空引用异常上的 Selecteditem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10244358/

相关文章:

wpf - 为什么在 MVVM 应用程序的 Controller 构造函数参数中使用 Func<>

c# - WPF TreeView 双击后恢复焦点

c# - MVVM Binding popover isOpen in datagrid

wpf - winform/WPF 应用程序的工作示例

java - Xamarin JAR 绑定(bind)未为 android.app.IntentService 的派生类创建绑定(bind)

wpf - 修改 TabControl 的标题

java - 不知道如何为 RecyclerView 注入(inject)适配器

c# - 可以使用 ReactiveUI 根据两个列表执行

c# - 如何在不需要任何外部连接的 WPF 应用程序中创建内部数据库?

c# - 在 XAML 中正确设置绑定(bind)源