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中,对应的委托执行中,进行判断源是否为空即可。

相关推荐
三千道应用题2 小时前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘20 小时前
开发WPF项目时遇到的问题总结
wpf
hqwest2 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars2 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest2 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest2 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0073 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度
范纹杉想快点毕业6 天前
C 语言主控开发与显控开发能力体系及技术栈详解,STM32、QT、嵌入式、边缘系统显示
stm32·单片机·tcp/ip·microsoft·fpga开发·51单片机·wpf
weixin_447103586 天前
WPF之绑定!
c#·wpf
DataIntel6 天前
wpf问题记录
wpf