WPF-DataGrid中的ComboBox的SelectionChanged事件以及数据更新问题记录

代码:

复制代码
			<DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox SelectedItem="{Binding LTFamilySymbol,UpdateSourceTrigger=PropertyChanged}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="SelectionChanged">
                                    <i:InvokeCommandAction Command="{Binding Path=DataContext.MyCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"></i:InvokeCommandAction>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn> 

问题描述:combobox里面的选项改变以后,后台获取不到该值,甚至连DataGrid的SelectedItem都获取错误(不是报错),只有当你进入本行的其他cell以后,本行才能被激活,才不会出错。

解决办法:其实是有一个属性没有设定,就是Combobox的selecteditem属性绑定时,需要添加updatesourcetrigger=propertychanged。这样只要combobox的选项发生改变,立马就知道了。

注意:combobox的selectedchanged事件,在窗口启动的时候,会自动执行一次,如果此时datagrid绑定的源为空,则会出问题。而在已有源的情况下,不会发生该问题。找到问题,解决办法就简单了:在ViewModel中,对应的委托执行中,进行判断源是否为空即可。

相关推荐
5***a97516 小时前
后端配置中心选型,Nacos与Apollo
wpf
·心猿意码·16 小时前
WPF转换器机制
wpf
她说彩礼65万19 小时前
WPF命令
wpf
玖笙&1 天前
✨WPF编程进阶【7.3】集成动画(附源码)
c++·c#·wpf·visual studio
ifeng09181 天前
鸿蒙分布式调试挑战:跨设备数据流转与连接稳定性
分布式·wpf·harmonyos
Macbethad2 天前
如何使用WPF做工控主页
wpf
Aevget2 天前
DevExpress WPF中文教程:Data Grid - Service(服务)示例
wpf·界面控件·devexpress·ui开发
Macbethad2 天前
WPF工控软件的设计方案
wpf
Macbethad4 天前
使用WPF编写一个读取串口的程序
wpf
Macbethad5 天前
如何用WPF做工控设置界面
java·开发语言·wpf