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 小时前
WPF中XAML的优点
wpf
无所谓จุ๊บ20 小时前
使用AI整理知识点--WPF动画核心知识
c#·wpf
xcLeigh2 天前
WPF在特定领域的应用:打造一款专业的图像编辑工具
c#·wpf
吾与谁归in2 天前
WPF给ListBox中的每一项添加右键菜单功能
c#·wpf
千里码!2 天前
RocketMQ延迟消息深度解析:原理、实践与性能调优
wpf·rocketmq
de之梦-御风3 天前
【.NET】WinForms 和 WPF 在性能方面的对比
.net·wpf
Zzu_zzx3 天前
c# wpf 开发中安装使用SqlSugar操作MySql数据库具体操作步骤保姆级教程
mysql·c#·wpf
白白白白纸呀3 天前
WPF框架---MvvmLight介绍
开发语言·c#·wpf
battlestar4 天前
WPF 解决加载顺序,Combox 增加属性,并关联text
windows·wpf
xcLeigh5 天前
WPF高级 | WPF 3D 图形编程基础:创建立体的用户界面元素
ui·3d·c#·wpf