WPF Databind Before Saving

前端 未结 12 2203
情深已故
情深已故 2020-11-29 03:46

In my WPF application, I have a number of databound TextBoxes. The UpdateSourceTrigger for these bindings is LostFocus. The object is saved using t

12条回答
  •  时光取名叫无心
    2020-11-29 04:02

    What do you think about this? I believe I've figured out a way to make it a bit more generic using reflection. I really didn't like the idea of maintaining a list like some of the other examples.

    var currentControl = System.Windows.Input.Keyboard.FocusedElement;
    if (currentControl != null)
    {
        Type type = currentControl.GetType();
        if (type.GetMethod("MoveFocus") != null && type.GetMethod("Focus") != null)
        {
            try
            {
                type.GetMethod("MoveFocus").Invoke(currentControl, new object[] { new TraversalRequest(FocusNavigationDirection.Next) });
                type.GetMethod("Focus").Invoke(currentControl, null);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to handle unknown type: " + type.Name, ex);
            }
        }
    }
    

    See any problems with that?

提交回复
热议问题