问题描述
问题:在SelectionChanged
的ComboBox的DataGrid
事件中,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" });
}
}
组合框SelectionChanged事件 [总是将cmbItem返回为空]:
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() + ".";
}
更改选择的快照:
组合框项显示为空:
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>
解决方法
((ComboBox)sender).SelectedItem as ComboBoxItem
是null
,因为SelectedItem不是ComboBoxItem。如果您明确添加了ComboBoxItems,例如,它将为ComboBoxItem。喜欢
<ComboBox>
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBox>
由于已将状态对象的集合辅助到ComboBox的ItemsSource属性,所以SelectedItem是状态对象:
private void cmbState_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
var state = (State)((ComboBox)sender).SelectedItem;
...
}
,
我无法添加评论,所以我在这里写下。
在microsoft的示例中,他们将类型ListBoxItem
的对象添加到列表框。因此,SelectedItem
可以在此处解析为ListBoxItem
。
但是您使用列表ListOfStates
,因此SelectedItem
的类型为State
。因此,您必须输入:State state = ((sender as ComboBox).SelectedItem as State);