【问题标题】:WPF Combox in DataGrid - SelectionChanged event returning selected ComboBoxItem as nullDataGrid 中的 WPF 组合框 - SelectionChanged 事件将选定的组合框项返回为空
【发布时间】:2020-12-14 10:16:02
【问题描述】:

问题:在DataGrid中Combobox的SelectionChanged事件内,ComboboxItem始终为空。我可能缺少什么以及我们如何解决这个问题?请注意(如图 1 所示)如果您能够从组合框中选择一个项目,则表示该组合框已经加载。因此在加载组合框后会调用 SelectionChanged 事件。

Employee.cs:

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string StateName { get; set; }
}

State.cs:

public class State
{
    public string StateName { get; set; }
    public string StateCode { get; set; }
}

StateList.cs:

public class StateList : List<State>
{
    public StateList()
    {
        Add(new State { StateName = "Iowa", StateCode = "IA" });
        Add(new State { StateName = "Nebraska", StateCode = "NE" });
        Add(new State { StateName = "Ohio", StateCode = "OH" });
        Add(new State { StateName = "Virginia", StateCode = "VA" });
    }
}

Combobox SelectionChanged 事件 [总是将 cmbItem 返回为 null]:

private void cmbState_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    StateList states = new StateList();
    string st = "";
    ComboBoxItem cmbItem = ((sender as ComboBox).SelectedItem as ComboBoxItem);
    if(cmbItem != null)
        st = "   You selected " + cmbItem.Content.ToString() + ".";
}

更改选择的快照:

ComboboxItem 显示为空:

MainWindow.xaml:

<Window x:Class="WPF_DataGridCombobox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        .......
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <local:StateList x:Key="ListOfStates"/>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="dgEmplyees" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Update">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="btnUpdate" Click="btnUpdate_Click"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding  Name}"/>
                <DataGridTemplateColumn Header="State Name">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding StateName}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="cmbState" ItemsSource="{StaticResource ListOfStates}" DisplayMemberPath="StateName" SelectedValuePath="StateName" SelectedValue="{Binding StateName}" SelectionChanged="cmbState_SelectionChanged" IsEditable="False" IsReadOnly="True" />                            
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

【问题讨论】:

  • (sender as ComboBox).SelectedItem as ComboBoxItem 为空,因为 SelectedItem 不是 ComboBoxItem。它是一个状态对象。
  • @Clemens 我以微软团队的this 官方文档为例。我误解了文件吗?

标签: c# wpf combobox datagrid datagridcomboboxcolumn


【解决方案1】:

我无法添加评论,所以我写在这里。 在microsoft 的示例中,他们将ListBoxItem 类型的对象添加到ListBox。因此SelectedItem 在这里可以被解析为ListBoxItem。 但是您使用列表ListOfStates,因此SelectedItem 的类型为State。因此你必须写:State state = ((sender as ComboBox).SelectedItem as State);

【讨论】:

  • 欢迎来到Stack Overflow。感谢您分享您的知识。这很有帮助。
【解决方案2】:
((ComboBox)sender).SelectedItem as ComboBoxItem

是 null,因为 SelectedItem 不是 ComboBoxItem。如果您明确添加了 ComboBoxItems,它将是一个 ComboBoxItem,例如喜欢

<ComboBox>
    <ComboBoxItem>Item 1</ComboBoxItem>
    <ComboBoxItem>Item 2</ComboBoxItem>
<ComboBox>

由于您已将一组 State 对象分配给 ComboBox 的 ItemsSource 属性,因此 SelectedItem 是一个 State 对象:

private void cmbState_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var state = (State)((ComboBox)sender).SelectedItem;
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2012-11-30
    相关资源
    最近更新 更多