Question

I want to write the contents of a per occasion active TextBox back to the bound property of the ViewModel when the user presses the key combination for save (Ctrl-S).

My Problem with it is, that I'm not able to trigger the execution of the binding so that the bound Text-Property reflects the contents of the TextBox.

-There seems to be no GetBinding-method. Therefore I can not get the Binding and execute it manualy.
-There is no Validate-method such as in WinForms which executes the Binding
-Giving focus to another control from within KeyDown seems not to work, the binding does not execute

How can I achieve this?

Was it helpful?

Solution

Take a look at Aaron's discussion about this in his WiredPrarie blog post : http://www.wiredprairie.us/blog/index.php/archives/1701

OTHER TIPS

I think I understand your question better now. One way around this would be to use a sub-classed textbox with a new property like this from here:

public class BindableTextBox : TextBox
{
    public string BindableText
    {
        get { return (string)GetValue(BindableTextProperty); }
        set { SetValue(BindableTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for BindableText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BindableTextProperty =
        DependencyProperty.Register("BindableText", typeof(string), typeof(BindableTextBox), new PropertyMetadata("", OnBindableTextChanged));

    private static void OnBindableTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
    {
        ((BindableTextBox)sender).OnBindableTextChanged((string)eventArgs.OldValue, (string)eventArgs.NewValue);
    }

    public BindableTextBox()
    {
        TextChanged += BindableTextBox_TextChanged;
    }

    private void OnBindableTextChanged(string oldValue, string newValue)
    {
        Text = newValue ? ? string.Empty; // null is not allowed as value!
    }

    private void BindableTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        BindableText = Text;
    }    
}

Then bind to the BindableText property.

Solution for command-instances
Here a solution I have found which is relatively leightweight, but also a bit "hackish":

btn.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);         
btn.Command.Execute(null);

First I give the focus to another control (In my case the button which has the bound command). Then I give the system time to execute the bindings and in the end I raise the command which is bound to the button.

Solution without bound commands
Give the Focus to another control and call the Dispatcher.ProcessEvent(...).

anotherControl.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
// Do your action here, the bound Text-property (or every other bound property) is now ready, binding has been executed

Please see also the solution of BStateham.
It's another way to solve the problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top