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.
It's working fine now.
Thank you!
Hi Marlon,
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.
Hi Rob,
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.