Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to get the value of textbox in ViewModel in WPF - MVVM

How to get the value of textbox in ViewModel in WPF - MVVM

Scheduled Pinned Locked Moved WPF
wpfcsharpphpcsscom
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rocky
    wrote on last edited by
    #1

    Hi guys, I'm working for the first time in WPF with MVVM pattern and I have a datagrid that is populated according to search terms in textbox on top. Right now, I'm able to populate the grid with all the data but I'm not able to get the value of the TextBox so I can filter it. So here's a bit of the code: In ViewModel:

    private string p_searchName;
    public string _searchName
    {
    get
    {
    return p_searchName;
    }
    set
    {
    p_searchName = value;

                base.RaisePropertyChanged("\_searchName");
            }
        }
    

    In xaml file

    Any help is greatly appreciated! Thanks

    Rocky My Blog

    J M 2 Replies Last reply
    0
    • R Rocky

      Hi guys, I'm working for the first time in WPF with MVVM pattern and I have a datagrid that is populated according to search terms in textbox on top. Right now, I'm able to populate the grid with all the data but I'm not able to get the value of the TextBox so I can filter it. So here's a bit of the code: In ViewModel:

      private string p_searchName;
      public string _searchName
      {
      get
      {
      return p_searchName;
      }
      set
      {
      p_searchName = value;

                  base.RaisePropertyChanged("\_searchName");
              }
          }
      

      In xaml file

      Any help is greatly appreciated! Thanks

      Rocky My Blog

      J Offline
      J Offline
      Jeremy Hutchinson
      wrote on last edited by
      #2

      The binding looks right, but are there any errors in the output window when loading that form? The other thing that it may be, is that the TextBox control doesn't update the ViewModel until it loses focus. Not sure about that though...

      My Blog[^] Chess Tactics for WP7[^]

      R 1 Reply Last reply
      0
      • R Rocky

        Hi guys, I'm working for the first time in WPF with MVVM pattern and I have a datagrid that is populated according to search terms in textbox on top. Right now, I'm able to populate the grid with all the data but I'm not able to get the value of the TextBox so I can filter it. So here's a bit of the code: In ViewModel:

        private string p_searchName;
        public string _searchName
        {
        get
        {
        return p_searchName;
        }
        set
        {
        p_searchName = value;

                    base.RaisePropertyChanged("\_searchName");
                }
            }
        

        In xaml file

        Any help is greatly appreciated! Thanks

        Rocky My Blog

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Make your binding Mode="TwoWay" default is only one way so you see the data but are not telling the datacontext the data has changed And yes the change is only notified when the user leaves the textbox

        Never underestimate the power of human stupidity RAH

        R 1 Reply Last reply
        0
        • M Mycroft Holmes

          Make your binding Mode="TwoWay" default is only one way so you see the data but are not telling the datacontext the data has changed And yes the change is only notified when the user leaves the textbox

          Never underestimate the power of human stupidity RAH

          R Offline
          R Offline
          Rocky
          wrote on last edited by
          #4

          Tried that too, didn't work unfortunately. :(

          Rocky My Blog

          P 1 Reply Last reply
          0
          • R Rocky

            Tried that too, didn't work unfortunately. :(

            Rocky My Blog

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            You need to set your UpdateSourceTrigger in your binding like this <TextBox Text="{Binding MyProperty, UpdateSourceTrigger=PropertyChanged}"/>

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            R 1 Reply Last reply
            0
            • J Jeremy Hutchinson

              The binding looks right, but are there any errors in the output window when loading that form? The other thing that it may be, is that the TextBox control doesn't update the ViewModel until it loses focus. Not sure about that though...

              My Blog[^] Chess Tactics for WP7[^]

              R Offline
              R Offline
              Rocky
              wrote on last edited by
              #6

              There are no errors in the output window. But you are quite right about the focus. I just added

              UpdateSourceTrigger=PropertyChanged

              like this

              <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>

              and it actually works... But I really don't see why this is so. Thanks for the help!

              Rocky My Blog

              J 1 Reply Last reply
              0
              • P Pete OHanlon

                You need to set your UpdateSourceTrigger in your binding like this <TextBox Text="{Binding MyProperty, UpdateSourceTrigger=PropertyChanged}"/>

                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                R Offline
                R Offline
                Rocky
                wrote on last edited by
                #7

                Yes you're right. Just did that a minute ago or so :) By the way... do you know why this is so? Just curious!

                Rocky My Blog

                W 1 Reply Last reply
                0
                • R Rocky

                  Yes you're right. Just did that a minute ago or so :) By the way... do you know why this is so? Just curious!

                  Rocky My Blog

                  W Offline
                  W Offline
                  Wayne Gaylard
                  wrote on last edited by
                  #8

                  The WPF textbox has a default UpdateSourceTrigger of LostFocus, which means that it only updates it's bound value when the user focuses on another control. By changing the trigger to update when the property changes, it updates it's bound value every time the user enters/deletes/changes a letter. Here is the MSDN page that discusses this How to: Control when the TextBox Text Updates the Source[^] . Hope this helps

                  When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                  R 1 Reply Last reply
                  0
                  • R Rocky

                    There are no errors in the output window. But you are quite right about the focus. I just added

                    UpdateSourceTrigger=PropertyChanged

                    like this

                    <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>

                    and it actually works... But I really don't see why this is so. Thanks for the help!

                    Rocky My Blog

                    J Offline
                    J Offline
                    Jeremy Hutchinson
                    wrote on last edited by
                    #9

                    I assume the default for UpdateSourceTrigger is LostFocus, and it is set that way for performance reasons. Every time you call the setter, the setter raises the OnPropertyChanged event, WPF listens to that event and calls the getter.

                    My Blog[^] Chess Tactics for WP7[^]

                    R 1 Reply Last reply
                    0
                    • W Wayne Gaylard

                      The WPF textbox has a default UpdateSourceTrigger of LostFocus, which means that it only updates it's bound value when the user focuses on another control. By changing the trigger to update when the property changes, it updates it's bound value every time the user enters/deletes/changes a letter. Here is the MSDN page that discusses this How to: Control when the TextBox Text Updates the Source[^] . Hope this helps

                      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                      R Offline
                      R Offline
                      Rocky
                      wrote on last edited by
                      #10

                      I see, thanks Wayne!

                      Rocky My Blog

                      W 1 Reply Last reply
                      0
                      • J Jeremy Hutchinson

                        I assume the default for UpdateSourceTrigger is LostFocus, and it is set that way for performance reasons. Every time you call the setter, the setter raises the OnPropertyChanged event, WPF listens to that event and calls the getter.

                        My Blog[^] Chess Tactics for WP7[^]

                        R Offline
                        R Offline
                        Rocky
                        wrote on last edited by
                        #11

                        Hmm.. thanks Jeremy! :)

                        Rocky My Blog

                        1 Reply Last reply
                        0
                        • R Rocky

                          I see, thanks Wayne!

                          Rocky My Blog

                          W Offline
                          W Offline
                          Wayne Gaylard
                          wrote on last edited by
                          #12

                          Great, Glad it helped :)

                          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups