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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. WPF Detect Property Change In Entity In VM

WPF Detect Property Change In Entity In VM

Scheduled Pinned Locked Moved WPF
questioncsharpwpfdesignhelp
12 Posts 4 Posters 6 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.
  • K Kevin Marois

    I have a ViewModel that contains a CompanyEntity. In the CompanyEntity is a property called IsActive. This property raises NotifyPropertyChanged. The property is bound to a checkbox in the UI. What I'd like is to somehow execute a method in the VM when the user changes the IsActive property via the checkbox. How do I do this? Thanks

    If it's not broken, fix it until it is

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

    There's a couple of ways you could do this. One way is to hook into the property itself and call the method when you're changing the value. The other way is to use RX to observe the property and call the method whenever the property changes. There's a pretty decent primer here[^] that should give you a heads up.

    K 1 Reply Last reply
    0
    • K Kevin Marois

      I have a ViewModel that contains a CompanyEntity. In the CompanyEntity is a property called IsActive. This property raises NotifyPropertyChanged. The property is bound to a checkbox in the UI. What I'd like is to somehow execute a method in the VM when the user changes the IsActive property via the checkbox. How do I do this? Thanks

      If it's not broken, fix it until it is

      M Offline
      M Offline
      Matt T Heffron
      wrote on last edited by
      #3

      I'd suggest looking into the PropertyObserver class from Josh Smith's MVVM Foundation library[^]. This will let you watch for the NotifyPropertyChanged event. Specific info at: One way to avoid messy PropertyChanged event handling[^]

      K 1 Reply Last reply
      0
      • K Kevin Marois

        I have a ViewModel that contains a CompanyEntity. In the CompanyEntity is a property called IsActive. This property raises NotifyPropertyChanged. The property is bound to a checkbox in the UI. What I'd like is to somehow execute a method in the VM when the user changes the IsActive property via the checkbox. How do I do this? Thanks

        If it's not broken, fix it until it is

        P Offline
        P Offline
        pradeep surya
        wrote on last edited by
        #4

        Hi, Simply you can achieve this by using triggers. Write an Event Trigger with event name as Click and bind an ICommand property which is in the viewmodel. So whenever there is a click on the checkbox(either select or deselect this method triggers) Then you can write the logic according to your functionality. There are so many ways to achieve this as well.

        1 Reply Last reply
        0
        • M Matt T Heffron

          I'd suggest looking into the PropertyObserver class from Josh Smith's MVVM Foundation library[^]. This will let you watch for the NotifyPropertyChanged event. Specific info at: One way to avoid messy PropertyChanged event handling[^]

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #5

          Well I implemented this and it doesn't work. I registered the property, compiled, and ran it. Nothing happens. The method I registered doesn't get called. The property IS changed, but the callback doesn't get called. Too bad, it looked promising. [UPDATE] So here's what I just found.... I have a GridView bound to a list of JobAssignmentEntities and who's SelectedItem is bound to a property in my VM called SelectedJobAssignment. In the CTOR of the VM I did

          propertyObserver = new PropertyObserver(this.SelectedJobAssignment)
          .RegisterHandler(n => n.PhaseStart, n => this.phaseStartChanged(n));

          In order to register it the property must not be null, because of a null ref check in the register method. So I moved the block above inside the SelectedJobAssignment's Getter and it works. sooo, I will need to find a way to register EACH time the SelectedJobAssignment is set.

          If it's not broken, fix it until it is

          M 2 Replies Last reply
          0
          • P Pete OHanlon

            There's a couple of ways you could do this. One way is to hook into the property itself and call the method when you're changing the value. The other way is to use RX to observe the property and call the method whenever the property changes. There's a pretty decent primer here[^] that should give you a heads up.

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #6

            Pete, Can you explain what you mean by 'Hook into the property'? Thanks

            If it's not broken, fix it until it is

            P 1 Reply Last reply
            0
            • K Kevin Marois

              Pete, Can you explain what you mean by 'Hook into the property'? Thanks

              If it's not broken, fix it until it is

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

              It's just a case of adding a piece of logic into your property setter and calling your method if the value has changed.

              1 Reply Last reply
              0
              • K Kevin Marois

                Well I implemented this and it doesn't work. I registered the property, compiled, and ran it. Nothing happens. The method I registered doesn't get called. The property IS changed, but the callback doesn't get called. Too bad, it looked promising. [UPDATE] So here's what I just found.... I have a GridView bound to a list of JobAssignmentEntities and who's SelectedItem is bound to a property in my VM called SelectedJobAssignment. In the CTOR of the VM I did

                propertyObserver = new PropertyObserver(this.SelectedJobAssignment)
                .RegisterHandler(n => n.PhaseStart, n => this.phaseStartChanged(n));

                In order to register it the property must not be null, because of a null ref check in the register method. So I moved the block above inside the SelectedJobAssignment's Getter and it works. sooo, I will need to find a way to register EACH time the SelectedJobAssignment is set.

                If it's not broken, fix it until it is

                M Offline
                M Offline
                Matt T Heffron
                wrote on last edited by
                #8

                Yes, this makes sense. The PropertyObserver constructor attaches to the current value (this.SelectedJobAssignment) of the passed property. It does not automatically track if that property itself changes. (I.e., it doesn't notice if this.SelectedJobAssignment changes) The registration tracks if the referenced property of that value changes. (I.e., it only notices if this.SelectedJobAssignment.PhaseStart changes)

                1 Reply Last reply
                0
                • K Kevin Marois

                  Well I implemented this and it doesn't work. I registered the property, compiled, and ran it. Nothing happens. The method I registered doesn't get called. The property IS changed, but the callback doesn't get called. Too bad, it looked promising. [UPDATE] So here's what I just found.... I have a GridView bound to a list of JobAssignmentEntities and who's SelectedItem is bound to a property in my VM called SelectedJobAssignment. In the CTOR of the VM I did

                  propertyObserver = new PropertyObserver(this.SelectedJobAssignment)
                  .RegisterHandler(n => n.PhaseStart, n => this.phaseStartChanged(n));

                  In order to register it the property must not be null, because of a null ref check in the register method. So I moved the block above inside the SelectedJobAssignment's Getter and it works. sooo, I will need to find a way to register EACH time the SelectedJobAssignment is set.

                  If it's not broken, fix it until it is

                  M Offline
                  M Offline
                  Matt T Heffron
                  wrote on last edited by
                  #9

                  Why not move it into the SelectedJobAssignment's Setter ?

                  K 1 Reply Last reply
                  0
                  • M Matt T Heffron

                    Why not move it into the SelectedJobAssignment's Setter ?

                    K Offline
                    K Offline
                    Kevin Marois
                    wrote on last edited by
                    #10

                    In ref to this and your prior posting.... I see how it works, it just looks kludgy. First, I could see something like

                    propertyObserver.Register(typeof(theType), x => x.Property, MyHandler());

                    this way you can register multiple type, and only need to have this on one place. Having to re-register each time the property changes is ugly

                    If it's not broken, fix it until it is

                    M 1 Reply Last reply
                    0
                    • K Kevin Marois

                      In ref to this and your prior posting.... I see how it works, it just looks kludgy. First, I could see something like

                      propertyObserver.Register(typeof(theType), x => x.Property, MyHandler());

                      this way you can register multiple type, and only need to have this on one place. Having to re-register each time the property changes is ugly

                      If it's not broken, fix it until it is

                      M Offline
                      M Offline
                      Matt T Heffron
                      wrote on last edited by
                      #11

                      You just use a different property observer. One for "the class" that checks when specific properties are changed. In the handlers for that observers, you register handlers for the properties on the values of the "properties of the class". This way the PropertyObserver handles the general case.

                      K 1 Reply Last reply
                      0
                      • M Matt T Heffron

                        You just use a different property observer. One for "the class" that checks when specific properties are changed. In the handlers for that observers, you register handlers for the properties on the values of the "properties of the class". This way the PropertyObserver handles the general case.

                        K Offline
                        K Offline
                        Kevin Marois
                        wrote on last edited by
                        #12

                        Ya I know. THe idea is great, just not the cleanest implementation.

                        If it's not broken, fix it until it is

                        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