WPF Databind Before Saving

前端 未结 12 2198
情深已故
情深已故 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?

    0 讨论(0)
  • 2020-11-29 04:02

    I'm using BindingGroup.

    XAML:

    <R:RibbonWindow Closing="RibbonWindow_Closing" ...>
    
        <FrameworkElement.BindingGroup>
            <BindingGroup />
        </FrameworkElement.BindingGroup>
    
        ...
    </R:RibbonWindow>
    

    C#

    private void RibbonWindow_Closing(object sender, CancelEventArgs e) {
        e.Cancel = !NeedSave();
    }
    
    bool NeedSave() {
        BindingGroup.CommitEdit();
    
        // Insert your business code to check modifications.
    
        // return true; if Saved/DontSave/NotChanged
        // return false; if Cancel
    }
    

    It should work.

    0 讨论(0)
  • 2020-11-29 04:04

    Assuming that there is more than one control in the tab sequence, the following solution appears to be complete and general (just cut-and-paste)...

    Control currentControl = System.Windows.Input.Keyboard.FocusedElement as Control;
    
    if (currentControl != null)
    {
        // Force focus away from the current control to update its binding source.
        currentControl.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        currentControl.Focus();
    }
    
    0 讨论(0)
  • 2020-11-29 04:04

    I've run into this issue and the best solution I've found was to change the focusable value of the button (or any other component such as MenuItem) to true:

    <Button Focusable="True" Command="{Binding CustomSaveCommand}"/>
    

    The reason it works, is because it forces the button to get focused before it invokes the command and therefore makes the TextBox or any other UIElement for that matter to loose their focus and raise lost focus event which invokes the binding to be changed.

    In case you are using bounded command (as I was pointing to in my example), John Smith's great solution won't fit very well since you can't bind StaticExtension into bounded property (nor DP).

    0 讨论(0)
  • 2020-11-29 04:08

    I found that removing the menu items that are scope depended from the FocusScope of the menu causes the textbox to lose focus correctly. I wouldn't think this applies to ALL items in Menu, but certainly for a save or validate action.

    <Menu FocusManager.IsFocusScope="False" >
    
    0 讨论(0)
  • 2020-11-29 04:11

    This is a UGLY hack but should also work

    TextBox focusedTextBox = Keyboard.FocusedElement as TextBox;
    if (focusedTextBox != null)
    {
        focusedTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
    }
    

    This code checks if a TextBox has focus... If 1 is found... update the binding source!

    0 讨论(0)
提交回复
热议问题