Search code examples
wpfuser-controlswpf-controlstabitem

set header of tabitem based on content in wpf


Initially I thought that it will be a very trivial functionality but now I am not able to implement it.

My requirement is simple. I am programmatically setting the content of a TabItem. The content will be usercontrols. I want to set the text of the header of the TabItem based on the content.

Content doesn’t have a changed event so I am confused as to on which event should I write code.

Also I’m not able to find any style or anything on the net.

Any suggestions? Please help. Thanks in advance.

PS: Please let me know if you need any further information from my side.


Solution

  • I think i got it. Not sure if this is the optimum solution so if anybody has a better solution than this then it will help me a lot.

    I made a custom tabitem and overrided OnContentChanged (Didnt knew that there is an overridable OnContentChanged :)). So my code is like below.

    
        public class TabItemData : TabItem
        {
            protected override void OnContentChanged(object oldContent, object newContent)
            {
                if (newContent.GetType().Name.ToLower().Contains("mycontrolname"))
                    this.Header = "control name";
                else
                    this.Header = "old name";
    
                base.OnContentChanged(oldContent, newContent);
            }
        }