I have a wpf View that is being hosted in a WinForm window. The save button is not in my view but it's in the winform window. When user type in the xamDataGrid text cell and click on the save button, the xamGrid is not getting the KillFocus event and therefore the source is not getting updated. I tried the following workaround but its not clean and not working quite right. I'm using Infragistics WPF controls v14.1
1. I created a TextEditor style with XamTextEditor and set the dirtyFlag on my viewModel OnTextChanged like so. I can't update the source OnTextChanged because it will trigger premature validation.
<Style TargetType="{x:Type dge:XamTextEditor}" x:Key="GridTextBoxStyle"> <Setter Property="EditTemplate"> <Setter.Value> <ControlTemplate TargetType="{x:Type dge:XamTextEditor}"> <TextBox Name ="txtFieldValue" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="FieldValue_TextChanged" /> </ControlTemplate> </Setter.Value> </Setter></Style>
<igWPF:XamDataGrid x:Name="IgDataGrid" DataSource="{Binding TimeCollection}" SelectedDataItemsScope="RecordsOnly" ActiveDataItem="{Binding SelectedTime}" EditModeStarted="DataGrid_EditModeStarted"> <igWPF:XamDataGrid.FieldLayouts> <igWPF:FieldLayoutKey="TimeColumns"> <igWPF:FieldLayout.Fields> <igWPF:Field Name="StartTime" Width="Auto" > <igWPF:Field.Settings> <igWPF:FieldSettings EditorStyle="{StaticResource GridTextBoxStyle}" EditorType="{x:Type dge:XamTextEditor}" EditAsType="{x:Type sys:String}" /> </igWPF:Field.Settings> </igWPF:Field> <igWPF:Field Name="Group" Width="Auto"> <igWPF:Field.Settings> <igWPF:FieldSettings EditorStyle="{StaticResource GridTextBoxStyle}" EditorType="{x:Type dge:XamTextEditor}" EditAsType="{x:Type sys:String}" /> </igWPF:Field.Settings> </igWPF:Field> </igWPF:FieldLayout> </igWPF:XamDataGrid.FieldLayouts></igWPF:XamDataGrid>
3. I also saved the LastActiveField OnEditModeStarted event so I can update the source manually when Save button is clicked:
private void DataGrid_EditModeStarted(object sender, Infragistics.Windows.DataPresenter.Events.EditModeStartedEventArgs e) { MyViewModel vm = ViewModelLocator.GetInstance().MyViewModel; if (vm != null) { vm.LastActiveField = e.Cell; vm.LastEditor = e.Editor; } }
3. When user Click Save button and if dirtyFlag is true it updates the source manually like so:
protected void UpdateLastField() { MyViewModel vm = ViewModelLocator.GetInstance().MyViewModel; if (vm != null && vm.LastActiveField != null) { if (vm.LastEditor != null) { if (vm.LastActiveField is Infragistics.Windows.DataPresenter.Cell) { var cell = (Infragistics.Windows.DataPresenter.Cell)vm.LastActiveField; ValueEditor editor = vm.LastEditor as ValueEditor; if (editor is XamTextEditor) { cell.Value = editor.Text; } editor.EndEditMode(true, true); } } } }
This whole thing feels like a hack and also when I tab from cell to cell I want the next cell to be in edit mode but it's not.
The cell in focused just have the dashed line around it.
A better solution is very much appreciated.
Hi Marlon,
I'm not sure if I'm doing anything incorrectly on the WinForms side of things, but I created a simple XamDataGrid inside of a UserControl and I gave the ElementHost this user control. I then added a WinForms button to the form. What I'm seeing in my sample is that clicking on the button causes the cell to exit edit mode which in turn commits the edited value to the underlying data source. In my sample when I click on the button, I display the results of the grid's data source. I can see that the values were updated. Was this not the case for you? If you run my sample do you see the desired behavior as I do?
Hi Rob,
Thanks for your reply. I just found out that these buttons not focusable. That's why I'm not getting the killfocus event. Given that is the case. Is there a better workaround than what I have? I need a better way to remember the last text cell and update it's source manually then when tabbing from cell to cell I need the text cell to be in EditMode right away.
Thanks for your help.
It's working fine now.
Thank you!
Let me know if you have any further questions on this matter.
With the way you have things setup, you should be able to use the XamDataGrid's CellChanged event rather than the TextChanged event on the TextBox in your template. In this case it can act like the TextChanged event.
With regards to putting the XamTextEditor into edit mode, that can be done using the ExecuteCommand method as well. There is a StartEditMode command you can use. Just set the grid's ActiveCell to the cell you want to edit and then execute the StartEditMode command. This will automatically put the cell's editor into edit mode.
That works. Now for the other problem. I'm using XamTextEditor for my cell so I can set the dirty flag OnTextChanged. Is there a similar event I can use in XamDataGrid to set Dirty flag without using the XamTextEditor? If not, then how do I get the XamTextEditor cell to be in EditMode when I tab from cell to cell?
I wouldn't worry about trying to remember what the last cell was or anything like that. The XamDataGrid has an EndEditModeAndAcceptChanges command that you can use to force the grid out of edit mode regardless of what cell is currently in edit mode and have the changes applied to the data source. You can execute this command using the XamDataGrid.ExecuteCommand method:
xamDataGrid1.ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges);
It should happen immediately so checking the data after this line should give you the updated values.