开发者

WPF bindings for settings read before execution

开发者 https://www.devze.com 2023-03-28 14:43 出处:网络
I have an application where bindings work almost as they should, there is just one problem: Some GUI elements update the underlying data type \"on exit\" i.e. when focusing on something else. However

I have an application where bindings work almost as they should, there is just one problem:

Some GUI elements update the underlying data type "on exit" i.e. when focusing on something else. However, this doesn't happend when I click "execute" or "save" (saving settings). So if the last setting that was set was a textbox and the user didn't click somewhere else, the updated setting value is not included into the execution / setting save.

  1. Is there a way to do this manually at execute/save?
  2. Why doesn't my clicking on execute/save work as a focus change? Maybe it does but it doesn't happend until after the event for the button i开发者_运维知识库s run?


You can use this to explicitly update the binding for a specific textbox element or the focused textbox element in your execute/save method and still use LostFocus for the UpdateSourceTrigger property:

public static void UpdateBinding()
{
    UpdateBinding(Keyboard.FocusedElement as TextBox);
}

public static void UpdateBinding(TextBox element)
{
    if (element != null) 
    {
        var binding = element.GetBindingExpression(TextBox.TextProperty);
        if (binding != null) 
        {
            binding.UpdateSource();
        }
    }
}


Try using UpdateSourceTrigger="PropertyChanged" on your textbox binding

EDIT

or use UpdateSourceTrigger="Explicit" and call the update method in your bindings when handling your button click

0

精彩评论

暂无评论...
验证码 换一张
取 消