Hello,
I have a XamDataGrid that binds to an object that have only a private constructor. So I had to create my own add new record button. Since I over wrote the add button already implemented in grid, I'm not getting focus on the new record.
1) How can I put the focus on the new added record. Please note that I'm using MVVM pattern.
2) I have set AddNewRecordLocation="OnTop" but the record is getting added on the button.
I'm wondering if someone looking into my question.
Thank you
Can you send a small sample project demonstrating the issue you are having? It's unclear exactly what the issue is. If all you want is to set to activate the first record in the grid you can set the IsActive property on the record.
this.xamDataGrid1.Records[0].IsActive = true;
Hi Michael,
As I specified in my original specs that I have a private constructor. This will make the plus sign on the xamdatagrid disappear. I can't make the constructor public due to design restrictions. I need to add a new record via a button (forget the Top requirement). How can I automatically focus on the New record using MVVM.
***Another issue with this xamdatagrid is that when you a new record with the add bottom and fill one cell or more and the delete key on the key board does not work anymore on the grid.
Hey Michael,
Did you check the behaviour of the xamdatagrid in the sample project I have submitted ? It is important that we resolve these issues ASAP.
The xamdatagrid is behaving strangely on the delete action as well.
Please address all issues I have pointed in my previous post.
The simplest way to get the add new row focus is to handle the grid's InitializeTemplateAddRecord event and force the first cell in the row into edit mode.
eg.
private void XamDataGrid1_InitializeTemplateAddRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeTemplateAddRecordEventArgs e) { var addRecord = e.TemplateAddRecord;
Dispatcher.BeginInvoke(new Action(() => { CellValuePresenter.FromCell(addRecord.Cells[0]).Editor.StartEditMode(); }), DispatcherPriority.Background, null); }
Keep in mind this only works if the row is in view. I am continuing to inject this into your sample. Let me know if this suites your requirements or have any additional questions.
Michael,
The code provided does not compile. Please added to the sample project I have provided.
You haven't addressed the delete issue.
User101 said: Michael, The code provided does not compile. Please added to the sample project I have provided. You haven't addressed the delete issue.
The BeginInvoke is not a static method. It need an instance of the object to reference it.
I found the issue with the weird behavior of the delete. I had UpdateMode="OnUpdate". Once I took it out, the delete worked fine.
I apologize for any confusion earlier. I’ve modified and attached your sample with my code snippet I provided earlier (tweaked) and fulfilled your requirements based on your original questions that you will find, in-line, below. If you have any questions please do not hesitate to ask. Note, when you run the sample and click the button it will put the newly added row at the first, at the top, and in edit mode.
1) How can I put the focus on the new added record? Please note that I'm using MVVM pattern.
Please handle your employee collection's CollectionChanged event and call the StartEditMode on the newly added record after confirming the newly added item is indeed the correct data record:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MainWindowViewModel view = this.Resources["myvm"] as MainWindowViewModel; view.ECollection.CollectionChanged += ECollection_CollectionChanged; }
private void ECollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.NewItems.Count == 1) { Dispatcher.BeginInvoke(new Action(() => { DataRecordCollection records = this.xamDataGrid1.Records as DataRecordCollection;
DataRecord record = records.GetDataRecordFromDataItem(e.NewItems[0], false) as DataRecord; if (record != null) { //Dispatcher.BeginInvoke(new Action(() => //{ CellValuePresenter.FromCell(record.Cells[0]).Editor.StartEditMode(); //}), DispatcherPriority.Background, null); } }), DispatcherPriority.Render); }
}2) I have set AddNewRecordLocation="OnTop" but the record is getting added on the button.To resolve this, please update your OnAdd method in your MainWindowViewModel.cs to use the Insert method at the index of zero: public void OnAdd(EmployeeCollection mycollection)
{ //mycollection.Add(Employee.CreateAnEmployee()); mycollection.Insert(0, Employee.CreateAnEmployee());
}
Let me know if you have any questions regarding this matter.
The attached sample project throws an exception when the Add button is clicked.
Did you test the solution ?
Yes I have.
No exception occurs when I click the add button. Please provide more details on the exception and clarify how to reproduce it and explain whether or not if this is occurring within your app or the sample. If the sample isn't working, which version of Infragistics are you using specifically? I'll need the four digit build number found in the details of the assemblies.
Let me know if you have any questions.
Hello Michael,
Exception is occurring in the sample project. The version is 16.1.20161.100.
Exception Details:
System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=xamDataGridSample StackTrace: at xamDataGridSample.MainWindow.<>c__DisplayClass1_0.<ECollection_CollectionChanged>b__0() in C:\Users\harbali\Desktop\xamDataGridSample\xamDataGridSample\xamDataGridSample\MainWindow.xaml.cs:line 50 at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at xamDataGridSample.App.Main() in C:\Users\harbali\Desktop\xamDataGridSample\xamDataGridSample\xamDataGridSample\obj\Debug\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: