嗨,我有一个 wpf 项目,它将数据网格绑定到这样的模块:
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Values">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
这工作正常。现在我想添加一些功能,包括将事件设置为组合框SelectionChanged
。所以我改变了xaml
这样的文件:
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Values">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
/!-- Add an EventSetter here. -->
<Style TargetType="{x:Type ComboBox}">
<EventSetter Event="SelectionChanged" Handler="ValueChanged" />
</Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
并在后端添加一个函数,如下所示:
private void ValueChanged(object sender, SelectionChangedEventArgs e)
{
var comboBox = sender as ComboBox;
if (comboBox.SelectedItem != null)
{
//do something here.
}
}
error: "The Application is in break mode."
但是,当我更改组合框单元格时,这会使项目崩溃。那么可能是什么问题呢?提前致谢!