none
Data Binding Mode=TwoWay LostFocus Necessary?

    Question

  • Hello,

    it seems that XAML only recognizes a two way binding back to to the source when a lost focus has been taken place? Is this so?

    But what to do when you change the only or last text box of the page and then click the Back App Bar Button?
    Hope you understand what i mean.

    Thank you,
    Markus

    Friday, March 17, 2017 3:10 PM

All replies

  • Check

    https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.updatesourcetrigger

    Friday, March 17, 2017 3:48 PM
  • When i try GetBindingExpression i get the error message:
    Error CS1503 Argument 1: cannot convert from 'string' to 'Windows.UI.Xaml.DependencyProperty'

    Ok, then all the properties of a class where a binding with textboxes exist must be not strings. They must be Dependency Properties to make the binding run????

    This can't be true. You change a value. Stay with the cursor in the TextBox and click the Back App Bar Button.
    Please explain how and why.

                if (this.Frame != null && this.Frame.CanGoBack) this.Frame.GoBack();
    
                // Offen: muss in die andere Seite.
                Entry_ViewModel.Entry_ViewModel_SingleInstance.
                Entries_Current_List_Entry_Selected = ViewModel;
    
                // BindingExpression
                BindingExpression be = Title.GetBindingExpression(ViewModel.Title);
                be.UpdateSource();
    

    Saturday, March 18, 2017 12:29 PM
  • And when i try a bit:
    Error Message by SetValue and GetValue
    Error CS0103 The name 'GetValue' does not exist in the current context 

    This can't be. What is the error? I have a page with one Text Box. Change the Text. Go Back. With the Top App Bar Button. This runs well under Visual Studio 2015 (not on the Windows 10 Phone). But not in Visual Studio 2017.

        public static readonly DependencyProperty SpeciesProperty =
        DependencyProperty.Register(
        "Species",
        typeof(String),
        typeof(Entry), null
        );
            public string Species
            {
                get { return (string)GetValue(SpeciesProperty); }
                set { SetValue(SpeciesProperty, (string)value); }
            }
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public string TextSimple1
            {
                get { return (string)GetValue(TextSimple1Property); }
                set { SetValue(TextSimple1Property, value); }
            }
    
            public static readonly DependencyProperty TextSimple1Property =
              DependencyProperty.Register("TextSimple1", typeof(string), typeof(Entry),
              new PropertyMetadata("Error."));
    
    


    Saturday, March 18, 2017 1:13 PM
  • The class must inherit from DependencyObject
    public class Entry : DependencyObject, INotifyPropertyChanged
     
    then the declaration of the DependancyProperty is possible.
     public static readonly DependencyProperty TitleProperty =
              DependencyProperty.Register("Title", typeof(string), typeof(Entry),
              new PropertyMetadata("Error."));

            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }

    But i don't know how to do UpdateSource
                // BindingExpression
                BindingExpression be = GetBindingExpression(ViewModel.Title);
                be.UpdateSource();
    error message:
    Error CS1503 Argument 1: cannot convert from 'string' to 'Windows.UI.Xaml.DependencyProperty' 

    the intention is simply to update source when you navigate with the mouse from the text box to the Back Button without explicitly change the focus with the mouse to a dummy textbox. But this seems necessary

     



    • Edited by Markus222 Saturday, March 18, 2017 7:00 PM
    Saturday, March 18, 2017 1:25 PM
  • Hello Markus222,

    Please see the document here:

    https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.bindingexpression#Windows_UI_Xaml_Data_BindingExpression_UpdateSource

    "You can obtain a BindingExpression object by calling the GetBindingExpression(DependencyProperty) method on an object, specifying the dependency property that has the binding."

    So you can call your method like this:

    sampletxt.GetBindingExpression(TextBox.TextProperty).UpdateSource();

    This is the normal way how we use it in our code. So in your method maybe you can try follow this pattern?

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 20, 2017 6:20 AM
    Moderator
  • Thank you.

    I think things changed with vs 2017. And i only do little programming from time to time. private, no matter, little. Some times for month nothing.

    So i'm not in there.

    Please tell me. Is Roslyn compiler necessary?

    There comes a Window that VS hat encountered that Features take time. Roslyn more than 20 seconds. And it schows the option to disable. I disabled it. From that time the program crash. No Dependency property possible. Some strange error message. Can't convert...

    Is it
    necessary? And how do i enable it again if so?

    If i have your answer i will try the other thing. But in the late evening today. Live in Germany.

    Monday, March 20, 2017 6:58 AM
  • Hello Markus222,

    >> Is Roslyn compiler necessary?

    Sorry I'm not so familar with it. So I cannot say whether it is necessary for Visual Studio 2017 or not. And if this happens for all your projects in Visual Studio you can try raise a new case on Visual Studio related forum instead. (And I can find Roslyn compiler from my installed extensions. If it does not affect your programming I recommend you keep it.)

    By the way, from your given code actually I recommend you give my sample code a try and see the result first. 

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 20, 2017 9:36 AM
    Moderator
  • i think i uninstall visual studio 2017 and install it new.

    i have no idea how to enable roslyn.

    therefore

    then try the dependency property and tell you the crash

    by the way in former days things mentioned in documentation did run in this way
    but today nearly nothing i don't know what they do

    Monday, March 20, 2017 9:47 AM
  • @Markus222,

    Well you can try to do a uninstall. And I've used a good uninstaller which works when using Visual Studio 2015.

    https://github.com/Microsoft/VisualStudioUninstaller

    Not sure if it works in Visual Studio 2017 RTM. For setup and installation we can try this forum.

    And you can also create a new project and see if the same problem persists. If the problem persist, maybe this is a environment issue of Visual Studio 2017.

    Do you have a simple repro so that I can test on my VS2017? 

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 20, 2017 9:57 AM
    Moderator
  • Hello Barry,

    thank you very much. Can i send you a zip file with email?
    it crashes, and i don't know why...

    it is a simple little project.

    my email address is MarkusS2001@gmx.de
    When you give me yours i make a send to zip folder in windows 10 and send it

    Thank you
    Markus


    • Edited by Markus222 Monday, March 20, 2017 3:42 PM
    Monday, March 20, 2017 3:41 PM
  • perhaps simply the registration of the dependency property is wrong.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    // INotifyPropertyChanged.
    using System.ComponentModel;
    
    using Windows.UI.Xaml;
    
    namespace P19.Entry
    {
        public class Entry : DependencyObject
        {
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Static Public / Internal Konstanten.
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // NCN = Namespace_Class_Name.
            public static readonly string NCN = string.Concat
                                                (typeof(Entry).Namespace, ".",
                                                 nameof(Entry));
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Event Deklaration.
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            public event PropertyChangedEventHandler PropertyChanged;
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Static Public / Internal Property Prozeduren.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // ID = "System.Guid.NewGuid().ToString().
            public static Entry Entry_Root { get; } =
                          new Entry()
                          {
                              ID = "cb8cc414-4cf2-4dc4-be04-0d2a2dcc965f",
                              Title = "Start"
                          };
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Private Property Variablen.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            private string iD_p;
    
            private string title_p;
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Public / Internal Property Prozeduren. 
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public String ID
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            {
                get { return iD_p; }
                set
                {
                    iD_p = value;
                }
            }
    
    
            public static readonly DependencyProperty TitleProperty =
              DependencyProperty.Register("Title", typeof(string),  typeof(Entry),
              null);
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
    
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Eigene Events auslösen.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    
        }
    }
    

    Monday, March 20, 2017 3:55 PM
  • without the dependency property it runs. But the behavior of the list view has to seem changed with 2017 it seams that some things are different from before, or i am false.

    This runs, but the Title Text Box needs Lost Focus to do the text change

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    // INotifyPropertyChanged.
    using System.ComponentModel;
    
    using Windows.UI.Xaml;
    
    namespace P19.Entry
    {
        public class Entry : INotifyPropertyChanged
        {
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Static Public / Internal Konstanten.
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // NCN = Namespace_Class_Name.
            public static readonly string NCN = string.Concat
                                                (typeof(Entry).Namespace, ".",
                                                 nameof(Entry));
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Event Deklarationen.
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            public event PropertyChangedEventHandler PropertyChanged;
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Static Public / Internal Property Prozeduren.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // ID = "System.Guid.NewGuid().ToString().
            public static Entry Entry_Root { get; } =
                          new Entry()
                          {
                              ID = "cb8cc414-4cf2-4dc4-be04-0d2a2dcc965f",
                              Title = "Start"
                          };
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Private Property Variablen.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            private string iD_p;
    
            private string title_p;
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Public / Internal Property Prozeduren. 
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public String ID
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            {
                get { return iD_p; }
                set
                {
                    iD_p = value;
                }
            }
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public String Title
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            {
                get { return title_p; }
                set
                {
                    title_p = value;
                    NotifyPropertyChanged("Title");
                }
            }
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Eigene Events auslösen.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // This method is called by the Set accessor of each property. 
            // The CallerMemberName attribute that is applied to the optional propertyName 
            // parameter causes the property name of the caller to be substituted as an argument. 
            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
        }
    }


    • Edited by Markus222 Monday, March 20, 2017 4:23 PM
    Monday, March 20, 2017 4:22 PM
  • @Markus222,

    We are not allowed to share email since this is not one to one support place. One to one support should create support ticket. For your project you can upload it to some online hard drive like OneDrive and then share me the link so that I can download it.

    Anyway, I think you need to understand something first:

    1. DependencyObject:https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.dependencyobject

    2. DependencyProperty:https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/dependency-properties-overview

    When checking DependencyProperty we can see this:

    "In order to support a dependency property, the object that defines the property must be a DependencyObject (in other words a class that has the DependencyObject base class somewhere in its inheritance)."

    So dependency property is always used in a custom control and in your case you don't need to use this.

    The binding only works when lose focus is normal for a textbox. When leaving focus it means you've left the editing and which can ensure the data will not be changed and the user left the input place. What you need to do is just adding the above code I mentioned in your OnHardwareButtonsBackPressed event. :

    yourtxt.GetBindingExpression(TextBox.TextProperty).UpdateSource();

    Per my understanding you will be able to update the source just using the above. There is no need for you to define your own dependency property here.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 21, 2017 2:36 AM
    Moderator
  • in the penultimate post there as a dependency property declared. I tried this declaration in different ways. But it allways crash. was not possible to instance an object.

    therefore my question was simply what was wrong. The class above did derive from dependenyobject. i don't want to read thousands pages of text. In Visual Basic Days you simply gave us a Plattform which were usable. But now you make it more and more complicate. every version more.

    no one is interested in this internas. We simply wanted to use it. i did not even see the parameters of DependencyProperty.Register Clearly Explained any more.

    Tuesday, March 21, 2017 5:23 AM
  • @Markus222,

    Actually Dependency object and dependency property was also introduced in WPF for so many years.

    It's a little hard for me to describe these things for you since I also only know the basic of it. But I will try introduce my understanding here:

    1. DependencyObject is a base. When we create custom control or user control it already inherit from DependencyObject. We don't need additional code.

    2. Dependency property: You can treat it as a different format of different property. It can be used in binding,animation and some functions like described hereMost of the time, when we check our XAML, it use Dependency property. But for binding source, dependency property is not required. 

    So let's go back to your first question:

    1. Why the source cannot be changed when not lose focus? 

    (I assume you've binding in the right way) Here the two way binding works only when data submitted(changed). And the UI system will validate all things after you textbox leave focus. So we should manually update the source if the system cannot specify it.

    2. "i did not even see the parameters of DependencyProperty.Register Clearly Explained any more."

    These things are listed on this page. I know the document contains a lot of text. But in this way we can use this better if we understand it. 

    Anyway, in normal developing I think we don't need to implement our own dp property if we just want to show some baisc things,like width,height. But in some advanced scenarios, for example, if we need to custom somethings ourselves, then dependency property is the thing that we need to understand. 

    If you have time I recommend that you use some time to read this document. After you've understand it you will find that your developing will be easier.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, March 21, 2017 9:36 AM
    Moderator
  • sorry i don't read it

    it was not possible to create an instance of this little class

    no valid window handle or so, i don't want to care about window handles....

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    // INotifyPropertyChanged.
    using System.ComponentModel;
    
    using Windows.UI.Xaml;
    
    namespace P19.Entry
    {
        public class Entry : DependencyObject
        {
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Static Public / Internal Konstanten.
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // NCN = Namespace_Class_Name.
            public static readonly string NCN = string.Concat
                                                (typeof(Entry).Namespace, ".",
                                                 nameof(Entry));
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Event Deklaration.
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            public event PropertyChangedEventHandler PropertyChanged;
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Static Public / Internal Property Prozeduren.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // ID = "System.Guid.NewGuid().ToString().
            public static Entry Entry_Root { get; } =
                          new Entry()
                          {
                              ID = "cb8cc414-4cf2-4dc4-be04-0d2a2dcc965f",
                              Title = "Start"
                          };
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Private Property Variablen.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            private string iD_p;
    
            private string title_p;
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Public / Internal Property Prozeduren. 
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public String ID
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            {
                get { return iD_p; }
                set
                {
                    iD_p = value;
                }
            }
    
    
            public static readonly DependencyProperty TitleProperty =
              DependencyProperty.Register("Title", typeof(string),  typeof(Entry),
              null);
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
    
    
    
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            // Eigene Events auslösen.
            // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    
        }
    }

    Tuesday, March 21, 2017 10:33 AM
  • @Markus222,

    I think we have a mis-understanding here but I don't know which problem is pending here. From your code I can test with Entry testentry = new Entry(); and it just cannot be used like the way you used. To use the GetBindingExpression we need to use FrameworkElement.GetBindingExpression, but Entry you defined is the dependencyObject which cannot use the method: GetBindingExpressioin.

    You can view this tree:

    DependencyObject

        UIElement

            FrameworkElement

    So back to original we just need to use our control's GetBindingExpressioin to get the BindingExpression and then call UpdateSource. Hope in this way you can understand.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    8 hours 57 minutes ago
    Moderator