MultiColumnComboBox 中的 SelectedItem 为 Null

SelectedItem in MultiColumnComboBox is Null

我正在 WPF 上写作。在其中我使用 MultiColumnComboBox 来 select 一些值。看起来像这样。

<ComboBox x:Name="OutputMatrNr" IsTextSearchEnabled="False" IsEditable="True" ItemsSource="{Binding DataSource}" IsDropDownOpen="False" StaysOpenOnEdit="True" KeyUp="OutputMatrNr_KeyUp" DropDownClosed="ComboBoxStudentOnDropDown" KeyDown="ComboBoxStudentOnKeyPress" SelectedItem="{Binding Path=students, Mode=TwoWay}" BorderThickness="2px" BorderBrush="black" Canvas.Left="2px" Canvas.Top="18px" Width="100px">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Margin="2" Text="{Binding MatrNr}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid x:Name="gd" TextElement.Foreground="Black">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Margin="5" Grid.Column="0" Text="{Binding MatrNr}"/>
                            <TextBlock Margin="5" Grid.Column="1" Text="{Binding NachName}"/>
                            <TextBlock Margin="5" Grid.Column="2" Text="{Binding VorName}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

这是背后的代码。

protected void ComboBoxStudentOnDropDown(object sender, EventArgs args)
    {
        if (((Student)OutputMatrNr.SelectedItem).Equals(null)){ // here sometimes the System.NullReferenceException occures
            FillStudentFromComboBox(((Student)OutputMatrNr.SelectedItem).MatrNr.ToString()); // I never(!) get here
        }
        else
        {
            Console.WriteLine("else");
        }
    }

    protected void ComboBoxStudentOnKeyPress(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            FillStudentFromComboBox(OutputMatrNr.Text); //this works every time
        }
    }

如果 ComboBox 中写入了 MatrNr,我会过滤其中写入的内容。如果您输入完整的 Nr 并按回车键,一切正常。 但是如果你点击一个学生,我总是在 return 中得到 Null(以 else 结尾)。我还得到一个 System.NullReferenceException 每隔一段时间就会出现一次,我无法准确地重现错误。 我遗漏了一些东西,但我无法弄清楚它是什么。

您的 OutputMatrNr.SelectedItem 可能为空。这就是您获得 NullReferenceException 的原因。所以IF条件应该改成这样。

if (OutputMatrNr.SelectedItem != null && OutputMatrNr.SelectedItem is Student)
{                
    FillStudentFromComboBox(((Student)OutputMatrNr.SelectedItem).MatrNr.ToString());
}
else
{
    Console.WriteLine("else");
}

此外,根据您的代码,SelectedItem 的类型为 Student。但是在 xaml 中,您将 SelectedItem 绑定到 students。如果是学生,那么你需要相应地改变xaml。

<ComboBox x:Name="OutputMatrNr" SelectedItem="{Binding Path=Student, Mode=TwoWay}"....>