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

相关推荐
bugcome_com15 小时前
WPF 命令 ICommand 从原理到实战
后端·wpf·icommand
武藤一雄1 天前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf
Venom841 天前
我的 WPF Powermill 工具
wpf
一念春风3 天前
证件照制作工具(WPF C#)
c#·wpf
He BianGu4 天前
【笔记】在WPF中GiveFeedbackEventHandler的功能和应用场景详细介绍
笔记·wpf
就是有点傻4 天前
WPF自定义控件-水晶球
wpf
He BianGu4 天前
【笔记】在WPF中QueryContinueDragEvent的详细介绍
笔记·wpf
He BianGu4 天前
【笔记】在WPF中QueryCursor事件的功能和应用场景详细介绍
笔记·wpf
He BianGu4 天前
【笔记】在WPF中CommandManager的功能和应用场景详细介绍
笔记·wpf
关关长语4 天前
HandyControl中Button图标展示多色路径
c#·.net·wpf·handycontrol