locked
How to handle property changed event and display message box RRS feed

  • Question

  • How to handle a property changed event on an entity and display message box dialog for user?
    Saturday, October 15, 2011 8:01 AM

Answers

  • I found solution my problem. I created INotifyPropertyChanged handler in GotFocus event. Not elegant, but works.

     

     

     partial void Tsk_ProjektBearbeiten_Created()
            {
     
                var control = this.FindControl("Name");
     
                control.ControlAvailable += new EventHandler<ControlAvailableEventArgs>(control_ControlAvailable);
            }
     
            void control_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {             TextBox txt = (TextBox)e.Control;             txt.GotFocus -= new RoutedEventHandler(txt_GotFocus);             txt.GotFocus += new RoutedEventHandler(txt_GotFocus);         }              void txt_GotFocus(object sender, RoutedEventArgs e)         {             Dispatchers.Main.BeginInvoke(() =>             {                 ((INotifyPropertyChanged)this.City).PropertyChanged -= MyScreen_PropertyChanged;                 ((INotifyPropertyChanged)this.City).PropertyChanged += MyScreen_PropertyChanged;             });         }
       void MyScreen_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                switch (e.PropertyName)
                {
                    case "Name": // action...
                        break;
                }
            }

     

    • Marked as answer by Marcin Sulecki Wednesday, October 19, 2011 8:25 AM
    Wednesday, October 19, 2011 8:25 AM

All replies

  • Hi Marcin

    Could you explain more about this please.

    When do display Message box to user, in wich screen, in which scenario?

    Spaso Lazarevic 

    Saturday, October 15, 2011 8:15 AM
  • You implement INotifyPropertyChanged. In the handler, you can do pretty much whatever you want (within the constraints of what's available in a screen), including displaying a message box, though I can't imagine why you'd want to do that, it would be fairly annoying for the user.

    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Saturday, October 15, 2011 8:31 AM
    Moderator
  • Hi Marcin again

    One of the recent example with INotifyPropertyChanged that I answer today: How to handle Property Changed Event in a grid

    Hope this help

    Spaso Lazarevic

    • Marked as answer by Marcin Sulecki Saturday, October 15, 2011 8:51 AM
    • Unmarked as answer by Marcin Sulecki Saturday, October 15, 2011 8:56 PM
    Saturday, October 15, 2011 8:33 AM
  • Hi Marcin

    If you have screen with property and you need to find out when it is changed, you don't need to remove and then add handler as in this sample, where we have Collection in grid.

    Screen template: Detail Screen

    Entity: City

    Properties: Prop1,Prop2,Prop3

    partial void CityDetail_Created()
    {
    	if (this.City == null)
    	{
    		return;
    	}
    
    	Dispatchers.Main.BeginInvoke(() =>
    	{
    		((INotifyPropertyChanged)this.City).PropertyChanged += PropertyChanged;
    	});
    }
    
    private void PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
    	switch (e.PropertyName)
    	{
    		case "Prop2":
    			this.ShowMessageBox("Property: " + e.PropertyName + " changed");
    			break;
    	}
    }
    

    When you change property Prop2 in detail screen you'll get MessageBox that property has changed.

    Hope this help

    Spaso Lazarevic

     

    • Marked as answer by Marcin Sulecki Saturday, October 15, 2011 7:49 PM
    • Unmarked as answer by Marcin Sulecki Saturday, October 15, 2011 8:55 PM
    Saturday, October 15, 2011 9:24 AM
  • Hi Spaso,

    Thank you for your example How to handle Property Changed Event in a grid. Well, it's not what it seems good works. In SelectionChanged event, the SelectedItem has just another row on data grid (usually next). Can you check it?

     

     

    Saturday, October 15, 2011 9:08 PM
  • Hi Spaso again,

    This example looks very nice. Unfortunetly when property is set by query, the property has null value on Created event. Anyone know solution?

     

    Saturday, October 15, 2011 9:21 PM
  • Yes Marcin,

    now I see. When you stay on the same row and change data everything is ok.

    But when you change data on row1 and go to row2, it affect row2 (row2.Prop3), not the old one where we change the data (row1).

    I'm trying to find the solution. Somehow we need to know is this new row or the same row. Maybe to use local screen property to record old row id and use that info as help to figure out which row need to be affect.

    If someone has better solution?

    Spaso Lazarevic

    Saturday, October 15, 2011 11:48 PM
  • Hi Spaso,

    When I can store old row to local property? I don't see suitable event for this.

     

    I suprised that it easy task is very hard in LightSwitch. Reportedly LS is tool for non-programmers...



    Monday, October 17, 2011 2:30 PM
  • Hi Marcin
    I'm not sure that this is most desirable solution but you could to something like this:
    1. Create Local Screen property (Add Data Item) with Integer type and put it in the screen but set Is Visible to false (this.Prop) Prop varibale is initial set to 0 by default.
    2. Then add variable in you class to store info is selection made in new row on the same row (newRow):
    Boolean newRow = false;
    partial void Cities_SelectionChanged()
    {
    	var selectedItem = this.Cities.SelectedItem;
    	if (selectedItem == null)
    	{
    		return;
    	}
    
    	if (this.Prop != 0 & this.Prop != this.Cities.SelectedItem.Id)
    	{
    		newRow = true; // New row
    	}
    	
    	this.Prop = this.Cities.SelectedItem.Id; //Current row. In Selection of new row this becomes old Row
    
    	Dispatchers.Main.BeginInvoke(() =>
    	{
    		((INotifyPropertyChanged)selectedItem).PropertyChanged -= SelectdItemPropertyChanged;
    		((INotifyPropertyChanged)selectedItem).PropertyChanged += SelectdItemPropertyChanged;
    	});
    }
    
    Then, you can use newRow in SelectdItemPropertyChanged method to check if after changing data User do selection in new row or make selection in the same row of the changed data.
    You can also use variable to store info about old Row Id and use it in method SelectdItemPropertyChanged.
    Hope this help
    Spaso Lazarevic
    Monday, October 17, 2011 5:28 PM
  • I found solution my problem. I created INotifyPropertyChanged handler in GotFocus event. Not elegant, but works.

     

     

     partial void Tsk_ProjektBearbeiten_Created()
            {
     
                var control = this.FindControl("Name");
     
                control.ControlAvailable += new EventHandler<ControlAvailableEventArgs>(control_ControlAvailable);
            }
     
            void control_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {             TextBox txt = (TextBox)e.Control;             txt.GotFocus -= new RoutedEventHandler(txt_GotFocus);             txt.GotFocus += new RoutedEventHandler(txt_GotFocus);         }              void txt_GotFocus(object sender, RoutedEventArgs e)         {             Dispatchers.Main.BeginInvoke(() =>             {                 ((INotifyPropertyChanged)this.City).PropertyChanged -= MyScreen_PropertyChanged;                 ((INotifyPropertyChanged)this.City).PropertyChanged += MyScreen_PropertyChanged;             });         }
       void MyScreen_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                switch (e.PropertyName)
                {
                    case "Name": // action...
                        break;
                }
            }

     

    • Marked as answer by Marcin Sulecki Wednesday, October 19, 2011 8:25 AM
    Wednesday, October 19, 2011 8:25 AM
  • This is basically what I suggested to you back in the beginning, but you're doing it in the wrong place.

    You need to take your code out of the TextBox's GotFocus handler & put it in the screen collection's SelectionChanged handler, as Spaso showed you before.

    Also, see this post: How To Handle Property Changed Event In A Grid

    Please unmark your "answer", because if anyone is looking for the answer to the same question, what you've done is not correct (even though it may "work"). You can hammer a nail in with a toaster, but you wouldn't want to make people think it's the right way to do it, would you? :-)

    Spaso's post is the one that should be marked as the answer. He gave you the answer, you just didn't do what he suggested.


    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Wednesday, October 19, 2011 11:14 AM
    Moderator
  • Thank you, but your suggestion and Spaso's solution dosn't work in my case. I get value of Property by Query.

    In SelectionChanged handler my Property is null yet.

     

    Wednesday, October 19, 2011 11:41 AM
  • Sorry, but you must be doing something "wrong" (like initialising in the wrong place, or something along those lines). That way *is* the correct way of doing it, & it *does* work.

    Can you explain what you mean by "I get value of Property by Query"?

    Btw, I'm trying to help you here, I'm not criticising you.


    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Wednesday, October 19, 2011 12:03 PM
    Moderator
  • Hi Yann,

    I know that you can help me :) Thank you. 

    I have screen with data grid and additional query with parameters. The query get single entity for selected row.

    I would like to handle specified property changed of this entity.

     

     

     

     

    Wednesday, October 19, 2011 12:29 PM
  • OK, I'm not sure why you're doing that (it sounds a little "odd" to be doing that), but it doesn't change anything to do with the actual technique. If you haven't already done so, make sure you add a screen property to hold the value that you get back from the query. Then just substitute the "selected item" in Spaso's code for the screen property. But I think that you still need to do it in the same place as we've been talking about, because the value will change every time the selection changes, yes? So you'll just be attaching he handler to a different object, the same as you were doing in your GotFopcus handler, but in the "right" place instead.

    Does that make sense?


    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Wednesday, October 19, 2011 1:12 PM
    Moderator