locked
ContentChanged Event for Labels in WPF RRS feed

  • Question

  • Hello,

    I am trying to make a custom Label control that fires an event whenever its content is changed in WPF.

    I found the following link: Label.Content Changed Event d

    However this has some issues:

    - The event fires when any custom label's content is changed (due to everything being declared static/shared)

    - The control does not render if I use "ContentProperty.OverrideMetadata" outside a static subroutine (Error: ArgumentException: PropertyMetadata is already registered for type [ObjectName])

    - I do not seem to understand the purpose of some of the code inside the sub "OnContentChanged"; could someone please clarify?

    My native language is VB.net, so if anything I wrote is incorrect or misinterpreted, please let me know.

    Any help would be greatly appreciated, thanks!

    Monday, October 27, 2014 1:41 AM

Answers

  • >>I am trying to make a custom Label control that fires an event whenever its content is changed in WPF.

    It is just a matter of overriding the OnContentChanged method in your custom Label class and raising an event in there:

      public class CustomLabel : Label
      {
        public event EventHandler ContentChanged;
    
        protected override void OnContentChanged(object oldContent, object newContent) {
          base.OnContentChanged(oldContent, newContent);
    
          if (ContentChanged != null)
            ContentChanged(this, EventArgs.Empty);
        }
      }
    

     

    <local:CustomLabel x:Name="test" ContentChanged="OnContentChanged"/>

        private void OnContentChanged(object sender, EventArgs e) 
        {
          //handle event
        }
    

    Please remember to mark helpful posts as answer and/or helpful.
    • Marked as answer by Blazing Ardor Tuesday, October 28, 2014 5:20 AM
    Monday, October 27, 2014 12:06 PM

All replies

  • In WPF you should probably be using TextBlock rather than Label.

    That's kind of minor though.

    A label is a read only control.

    The user isn't going to change the data.

    Whatever code you have which provides the data will be changing the data.

    What I don't quite follow is why would you want such an event anyhow?

    In WPF I would expect you to bind a Textblock Text property or Label content property.

    The simplest way to do that is to a class implementing inotifypropertychanged.

    The text/content would be bound to a public property.

    The place to detect changes is the setter of such a property.

    ( You can also use dependency properties which have a callback for changes ).

    In c# that would look like:

            private string myString;
            public string MyString
            {
                get
                {
                    return myString;
                }
                set
                {
                    if(myString != value)
                    { 
                        myString = value;
                        DoSomething(myString);
                    }
                }
            }
    In this code, when your string is changed then the DoSomething method is called

    Even if you decided not to use MVVM ( which would be a bit crazy, by the way ) then you would have some code setting the text/content property of the control. Do whatever you need to do there.

    Monday, October 27, 2014 8:54 AM
  • >>I am trying to make a custom Label control that fires an event whenever its content is changed in WPF.

    It is just a matter of overriding the OnContentChanged method in your custom Label class and raising an event in there:

      public class CustomLabel : Label
      {
        public event EventHandler ContentChanged;
    
        protected override void OnContentChanged(object oldContent, object newContent) {
          base.OnContentChanged(oldContent, newContent);
    
          if (ContentChanged != null)
            ContentChanged(this, EventArgs.Empty);
        }
      }
    

     

    <local:CustomLabel x:Name="test" ContentChanged="OnContentChanged"/>

        private void OnContentChanged(object sender, EventArgs e) 
        {
          //handle event
        }
    

    Please remember to mark helpful posts as answer and/or helpful.
    • Marked as answer by Blazing Ardor Tuesday, October 28, 2014 5:20 AM
    Monday, October 27, 2014 12:06 PM
  • Ahh, thanks for clearing that up. The primary reason I needed an event is because I programatically change the labels, and I needed to know whether or not the label was clipped off by the parent control when a change to it was made. I found that using the SizeChanged event worked for this purpose, but what you explained definitely helped out if I ever need to create an event if it is not present in the default class.

    Thanks for your help :)

    Tuesday, October 28, 2014 5:20 AM