locked
INotifyPropertyChanged plus a local property RRS feed

  • Question

  • Hi,

    I have a datagrid with several fields and 1 autocompletebox called field1. When the user change the autocomplete box field 1, I update another field (field2) in the same datagrid with a property methods changed applied on field1. As soons as the user update the field1, I would like to messagebox at the user that that field2 has been updated and let the user check the content. Below is the example I'm following (thanks to Spaso!)

    Screen template: Detail Screen

    Entity: City

    Properties: Prop1,Prop2,Prop3

    partialvoid CityDetail_Created() { if (this.City == null) { return; } Dispatchers.Main.BeginInvoke(() => { ((INotifyPropertyChanged)this.City).PropertyChanged += PropertyChanged; }); } privatevoid PropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case"Prop2":

    // Here I want something like this

    this.MyLocalPorperty = 1;

    this.ShowMessageBox("Property: " + e.PropertyName + " changed"); break;

    case "Prop3":

    if (this.MyLocalProperty ==1)

    this.ShowMessageBox("OK");

    break;

    }

    }

    Seems that the local properties don't work inside that!

    Thanks

    LightBoom





    • Edited by LightBoom Monday, December 17, 2012 2:32 AM
    Monday, December 17, 2012 2:26 AM

Answers

  • "local properties don't work inside that"

    What does that mean? What error message do you get?

    You might need to surround the code causing the error with a dispatcher BeginInvoke call (similar to what's being done in the screen's Created method.

    If it's the local property assignment line, you'd need:

    Dispatchers.Main.BeginInvoke(() =>
        {
            this.MyLocalProperty = 1;
        });

    If it's the message box line, you'd need:

    this.Details.Dispatcher.BeginInvoke(() =>
        {
            this.ShowMessageBox("OK");
        });


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Marked as answer by LightBoom Friday, December 21, 2012 7:50 PM
    Monday, December 17, 2012 3:12 AM
    Moderator
  • I can see one problem. In fact your code, as it is, should only work for one row, being the first selected item in the collection. The collection data may not even be finished loading at that point either. Only changes to properties in that initially selected row will trigger your handler code. When a different item is selected, the handler code won't be triggered for the properties in the newly selected row.

    If you're wanting to monitor the changes to the properties of the property that a detail screen is bound to, you'd put adding the handler in the screen's Created method.

    But if you're wanting to monitor the changes to the properties of a collection's SelectedItem, then you need to add the handler in the collection's SelectionChanged method.

    Have a look at the code & explanations in these two threads to see the difference.

    How to do Screen only Validation in Property/Field Level (for detail screen)

    How to handle Property Changed Event in a grid (for a collection)


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Marked as answer by LightBoom Saturday, December 22, 2012 4:03 PM
    Saturday, December 22, 2012 4:46 AM
    Moderator

All replies

  • "local properties don't work inside that"

    What does that mean? What error message do you get?

    You might need to surround the code causing the error with a dispatcher BeginInvoke call (similar to what's being done in the screen's Created method.

    If it's the local property assignment line, you'd need:

    Dispatchers.Main.BeginInvoke(() =>
        {
            this.MyLocalProperty = 1;
        });

    If it's the message box line, you'd need:

    this.Details.Dispatcher.BeginInvoke(() =>
        {
            this.ShowMessageBox("OK");
        });


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Marked as answer by LightBoom Friday, December 21, 2012 7:50 PM
    Monday, December 17, 2012 3:12 AM
    Moderator
  • Hi Yann,

    Thank you for your support, now everything is ok. Below is my NEW code please review it and yell me if I can do better. Just to better understand, when I have to refer to the UI thread I have to use the dispatchers, but when I have to use this.Details.Dispatcher.BeginInvoke(() => and when I have to use Dispatchers.Main.BeginInvoke(() => ? Sorry for this question, but I'm confused about Dispatchers. Thank you

    LightBoom

    partial voidCLsListDetail_Created()

     {

    if (this.RIFs.SelectedItem == null)  

              {

    return;

                }

               

    Dispatchers.Main.BeginInvoke(() =>

                {

                    ((

    INotifyPropertyChanged)this.RIFs.SelectedItem).PropertyChanged -= PropertyChanged;

                    ((

    INotifyPropertyChanged)this.RIFs.SelectedItem).PropertyChanged += PropertyChanged;      

                   

    this.mstFlagArtChanged = 0;

                });

            }

           

    privatevoidPropertyChanged(objectsender, PropertyChangedEventArgse)

            {             

               

    switch(e.PropertyName)

                {

                   

    case"Prop1":

                        {

                           

    this.Details.Dispatcher.BeginInvoke(() =>

                            {

                               

    this.mstFlagArtChanged = 1;

                               

    this.ShowMessageBox("Property: "+ e.PropertyName + " changed"+ this.mstFlagArtChanged);

                            });

                           

    break;

                        }

                   

                   

    case"Prop2":

                        {

                           

    this.Details.Dispatcher.BeginInvoke(() =>

                            {

                               

    if(this.mstFlagArtChanged == 1)

                                   

    this.ShowMessageBox("Property: "+ e.PropertyName + " changed"+ this.mstFlagArtChanged);

                                mstFlagArtChanged = 0;

                            });

                           

    break;

                        }

                }

            }

    return;
    If(this.RIFs.SelectedItem == null)



    • Edited by LightBoom Monday, December 17, 2012 12:35 PM
    Monday, December 17, 2012 12:23 PM
  • I can see one problem. In fact your code, as it is, should only work for one row, being the first selected item in the collection. The collection data may not even be finished loading at that point either. Only changes to properties in that initially selected row will trigger your handler code. When a different item is selected, the handler code won't be triggered for the properties in the newly selected row.

    If you're wanting to monitor the changes to the properties of the property that a detail screen is bound to, you'd put adding the handler in the screen's Created method.

    But if you're wanting to monitor the changes to the properties of a collection's SelectedItem, then you need to add the handler in the collection's SelectionChanged method.

    Have a look at the code & explanations in these two threads to see the difference.

    How to do Screen only Validation in Property/Field Level (for detail screen)

    How to handle Property Changed Event in a grid (for a collection)


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Marked as answer by LightBoom Saturday, December 22, 2012 4:03 PM
    Saturday, December 22, 2012 4:46 AM
    Moderator
  • Hi Yann,

    thank for your code review, you are right (as always...), the code needed to use the SelectionChanged event.

    Regards

    LightBoom

    Saturday, December 22, 2012 4:02 PM
  • You're welcome!

    As for "you are right (as always...)", I wish, lol.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Sunday, December 23, 2012 10:42 AM
    Moderator