Home Forums WPF controls Xceed DataGrid for WPF Switching Tabs Grid Focus Issue

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26696 |

    I have a grid on in a tab. When I’m editing a cell and then select a different tab on the grid, I’d like to save what the user was entering before I switch to the tab. However, I can’t figure out which event to capture as none of the focus related events get fired. Is there a recommended Xceed best practice for handling this type of situation?

    Thanks!

    Paul

    Imported from legacy forums. Posted by Paul (had 4664 views)

    User (Old forums)
    Member
    Post count: 23064

    Do you mean you have a DataGrid on a TabControl?
    Because you say this: “grid on in a tab” and “different tab on the grid”. Do you mean a columns or TabControls?

    If you mean that you have a DataGrid on a TabControl and you want to switch to a different page in that TabControl and to detect when this happens….then you are in the same boat as I am. The DataGrid never fires off a LostFocus event when you click on a control outside the DataGrid. I have asked about this but no one ever replied to me on this.

    Furthermore if you have a child relation in your DataGrid and the user is focused on it and then leaves the DataGrid by clicking on another control that wants to alter that row programmatically…..you can’t because that row remains stuck in the Edit mode.

    Imported from legacy forums. Posted by David (had 667 views)

    User (Old forums)
    Member
    Post count: 23064

    Correct, I have a DataGrid on a tab control. When I select another tab while editing a cell in the grid control the DataGrid doesn’t lose focus.

    Well, maybe it does but just doesn’t let me know, because when I come back to the tab the grid is on, the DataGrid is no longer in edit mode and the data that was entered is replaced with the old value. i.e. unchanged.

    So, we are indeed in the same boat!

    Imported from legacy forums. Posted by Paul (had 636 views)

    Xceed Support
    Member
    Post count: 5658

    What is happening is that the grid is unloaded when the selected tab changes. If the grid is unloaded, it also exists edit mode.

    That said, the only workaround would be to implement your own tab-switching mechanism or to make sure EndEdit is called before switching tabs.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 490 views)

    User (Old forums)
    Member
    Post count: 23064

    I think there is a bug in the method in EndEdit().

    If you are focused on a detail row and call that method you get a exception. (It works fine when you are pointed at the top master row)

    Here is the exception:

    System.InvalidOperationException was unhandled
    Message=”An attempt was made to edit an item that is not part of the specified context.”
    Source=”Xceed.Wpf.DataGrid”
    StackTrace:
    at Xceed.Wpf.DataGrid.DataGridControl.EndEditHelper(DataGridContext dataGridContext)
    at Xceed.Wpf.DataGrid.DataGridControl.EndEdit()
    at iGlimpse2.Window1.Button_Complete_Task_Site_ID_Click(Object sender, RoutedEventArgs e) in C:\expression\projects\iGlimpse2\Window1.xaml.cs:line 934
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(RoutedEventArgs args)
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
    at System.Windows.Controls.Button.OnClick()
    at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
    at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
    at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.ReRaiseEventAs(RoutedEventArgs args, RoutedEvent newEvent)
    at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
    at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(RoutedEventArgs args)
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
    at System.Windows.Input.InputManager.ProcessStagingArea()
    at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
    at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
    at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
    at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
    at System.Windows.Interop.HwndSource.InputFilterMessage(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, Boolean isSingleParameter)
    at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
    at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
    at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
    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.Threading.Dispatcher.Run()
    at System.Windows.Application.RunInternal(Window window)
    at System.Windows.Application.Run(Window window)
    at System.Windows.Application.Run()
    at iGlimpse2.App.Main() in C:\expression\projects\iGlimpse2\obj\Debug\App.g.cs:line 0
    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
    at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
    at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
    at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
    at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
    at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:

    Imported from legacy forums. Posted by David (had 688 views)

    Xceed Support
    Member
    Post count: 5658

    This is normal behavior if you are trying to call EndEdit on an item that is not part of the appropriate context. That said, get the DataGridContext for the item and call the context’s EndEdit method to end the edit process.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 4203 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jenny

    You mentioned that the DataGrid is unloaded when it is not visible (switching tabs in tabcontrol). For my framework I need to know when this happens is there an event that is fired which I could catch? or better is there a way to prevent it from happening, if possible I don’t want the cells to be recreated everytime I switch the tab (only if the datasource changes)(does it happens on vertical scroll?). In winforms I would do my cleanup in dispose but in wpf the dispose concept is a foreign word it seems.

    thanks in advance and Great DataGrid[ip]

    Imported from legacy forums. Posted by ステファン (had 930 views)

    User (Old forums)
    Member
    Post count: 23064

    The following works for me:

    (It detects when focus switches from the grid to another tab, somewhere else in the window, etc)

            private void KeyboardFocusWithinChangedEvent(object sender, DependencyPropertyChangedEventArgs e)
            {
                if (e.NewValue.ToString() == bool.FalseString)
                {
                    Xceed.Wpf.DataGrid.DataRow dr = this.GetContainerFromItem(this.CurrentItem) as Xceed.Wpf.DataGrid.DataRow;
                    if (dr != null)
                    {
                        dr.EndEdit();
                        //Save row….
                    }
                }
            }
     

    Imported from legacy forums. Posted by Serene (had 1475 views)

    User (Old forums)
    Member
    Post count: 23064

    This looks like a good solution for a similar problem – how did you wire up this event handler?

    Imported from legacy forums. Posted by Adam (had 620 views)

    User (Old forums)
    Member
    Post count: 23064

    Ha – leave me alone long enough and I’ll figure it out… [:P]

    The DataGridControl exposes this event:

    IsKeyboardFocusWithinChanged

    Wire it up and that’s it!

      

     

    Imported from legacy forums. Posted by Adam (had 1799 views)

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.