none
Force tooltip update RRS feed

  • Question

  • I have a class such as this

    class Staff
    {
        public string Name {get;set;}
        public string Address {get;set;}
        public string ToolTipText
        {
              get { return "Name: " + Name + "\r\nAddress: " + Address +. .... etc; }

         }

    }

     

    I use the ToolTipText property to bind to my tooltip:

    <ToolTip>
        <TextBlock Text={Binding ToolTipText}/>

    </ToolTip>

    The problem I have is that when someone's name is changed the tooltip does not update. I could raise a propertyChanged event when it changes but that means raising it for every single property on mt Staff class. This gets tedious for classes that have a lot of properties. It also seem unnecessary to me because I would have thought the tooltip would request the value from the class each time it is shown. So that is my question, how do I get the tooltip to call ToolTipText property every time it is shown?

     

    Thanks,

    Michael

    Tuesday, July 12, 2011 6:44 AM

Answers

  • Hi Srinu,

     

    Thanks for the reply. As I said in my initial question, I want to avoid raising propertychanged event because it gets tedious to do this for every single property. The way you have written it tooltip can be written to but in my case tooltip is a read only property that is generated on the fly. It changes when any property of the Staff object changes so I need to fire propertychanged for every single property on staff. If I have 50 classes in my app with 30 properties each then I need to add code to fire the tooltip changed event 1500 times!! This is what I want to avoid. It seems to unnecessary to me because the tooltip could refresh itself each time it is shown. Somehow it is caching data somewhere.

    As an example, this is some of the code in my Person class

     

        partial class Person : IPhotoItem
        {
            partial void OnDateDeletedChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnDateOfBirthChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnFamilyNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnFirstNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnGenderIDChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnMiddleNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnPreferredNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

    Tuesday, July 12, 2011 11:03 PM

All replies

  • Maybe you should implement INotifyPropertyChanged in your class.

    It would look something like this.
    Add - using System.ComponentModel;

    And then set your class up like this. 

     public class Staff : INotifyPropertyChanged
      {
        public string Name { get; set; }
        public string Address { get; set; }
        private string _tooltipText = "";
        public string TooltipText
        {
          get { return _tooltipText; }
          set
          {
            _tooltipText = value;
            RaisePropertyChanged("TooltipText");
          }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
          if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
      }
    


    This updates the value to the binding everytime it updates. :)
    Try this out. 


    Developing is part of being a developer.
    • Proposed as answer by Srinu Mutyala Tuesday, July 12, 2011 12:11 PM
    Tuesday, July 12, 2011 7:20 AM
  • Hi Srinu,

     

    Thanks for the reply. As I said in my initial question, I want to avoid raising propertychanged event because it gets tedious to do this for every single property. The way you have written it tooltip can be written to but in my case tooltip is a read only property that is generated on the fly. It changes when any property of the Staff object changes so I need to fire propertychanged for every single property on staff. If I have 50 classes in my app with 30 properties each then I need to add code to fire the tooltip changed event 1500 times!! This is what I want to avoid. It seems to unnecessary to me because the tooltip could refresh itself each time it is shown. Somehow it is caching data somewhere.

    As an example, this is some of the code in my Person class

     

        partial class Person : IPhotoItem
        {
            partial void OnDateDeletedChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnDateOfBirthChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnFamilyNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnFirstNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnGenderIDChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnMiddleNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

            partial void OnPreferredNameChanged()
            {
                base.OnPropertyChanged("ToolTip");
            }

    Tuesday, July 12, 2011 11:03 PM
  • Hi MickleKulls,

    Based on your description, I am not sure if you have got the solution, however, I think follow SharpAspirant's suggestion(complete INotifyPropertyChanged interface) will not cost so much, if the Tooltip is not visible, it will not refresh or updatelayout, if the tooltip become visible, it will get the new value.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 4:05 AM
    Moderator