Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6968451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:27:13+00:00 2026-05-27T16:27:13+00:00

I am trying to change my labels style dynamically on my forms. The behaviour

  • 0

I am trying to change my labels style dynamically on my forms.
The behaviour I want is: Every time a textbox called ‘txtName’, for instance, gets Focused, it should search for a Label Control named ‘lblName’ and change its FontWeight property to “Bold”.

The same for a textbox called ‘txtBirthday’ and a label called ‘lblBirthday’, where ‘txt’ stands for TextBox and lbl for Label.

Every textbox has a NAME and a prefix “txt” and a prefix “lbl” for its corresponding label, but if the textbox doesnt find a correspoding label it should do nothing.

In other words, every time a Textbox get focused on the form, it should search for the label “responsable” for its description and hightlight it (changing its font weight to bold) so the form will be more user frendly. That way the user wont get confused which textbox he is typing in.

I have a peace of code that maybe a good start point, but I dont know how to work with non-static control names.

    <Style TargetType="{x:Type Label}">

    <Style.Triggers>
        <!-- Here is how we bind to another control's property -->
        <DataTrigger Binding="{Binding IsFocused, ElementName=txtUser}" Value="True">
            <Setter Property="FontWeight" Value="Bold" />
            <!-- Here is the 'override' content -->
        </DataTrigger>

    </Style.Triggers>

</Style>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:27:13+00:00Added an answer on May 27, 2026 at 4:27 pm

    As mentioned in the comments above, the technique of searching for and pattern matching element names as the basis for applying visual behaviour is not robust. For example, what happens when you make a typo and use “lbel” instead of “lbl”? Or what happens if you later decide to replace all Labels with TextBlocks – do you still annotate their names with a prefix of “lbl” to preserve the behaviour? Another downside to using code to change visuals – is now understanding the behaviour of your UI from reading XAML alone becomes much harder since properties are being changed behind the scenes. WPF has many built in ways which should be preferred over this approach. If you are interested in alternative implementations, just ask we are here to help 🙂

    That being said, if must use this approach, here is what your attached behaviour would look like:

    C#

    public static class FontWeightFocusedHelper
    {
        private static readonly List<Label> Labels = new List<Label>();
    
        public static void SetChangeFontWeightOnTextBoxFocused(Label label, bool value)
        {
            label.SetValue(ChangeFontWeightOnTextBoxFocusedProperty, value);
        }
    
        public static bool GetChangeFontWeightOnTextBoxFocused(Label label)
        {
            return (bool) label.GetValue(ChangeFontWeightOnTextBoxFocusedProperty);
        }
    
        public static readonly DependencyProperty ChangeFontWeightOnTextBoxFocusedProperty =
            DependencyProperty.RegisterAttached("ChangeFontWeightOnTextBoxFocused", typeof (bool),
                                                typeof (FontWeightFocusedHelper),
                                                new FrameworkPropertyMetadata(OnChangeFontWeightOnTextBoxFocusedPropertyChanged));
    
        private static void OnChangeFontWeightOnTextBoxFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TextBox)
            {
                var textBox = (TextBox) d;
                // Make sure to use a WeakEventManager here otherwise you will leak ...
                textBox.GotFocus += OnTextBoxGotFocusChanged;
                textBox.LostFocus += OnTextBoxLostFocusChanged;
                return;
            }
    
            if (d is Label)
            {
                // Make sure to store WeakReferences here otherwise you will leak ...
                Labels.Add((Label)d);
                return;
            }
    
            throw new InvalidOperationException("ChangeFontWeightOnTextBoxFocused can only be set on TextBox and Label types.");
        }
    
        private static void OnTextBoxLostFocusChanged(object sender, RoutedEventArgs e)
        {
            SetMatchingLabelFontWeight(sender as TextBox, FontWeights.Regular);
        }
    
        private static void OnTextBoxGotFocusChanged(object sender, RoutedEventArgs e)
        {
            SetMatchingLabelFontWeight(sender as TextBox, FontWeights.Bold);
        }
    
        private static void SetMatchingLabelFontWeight(TextBox textBox, FontWeight fontWeight)
        {
            if (textBox != null)
            {
                // Suggest adding a property for LabelPrefix and TextBoxPrefix too, use them here
                var label = Labels.Where(l => !String.IsNullOrEmpty(l.Name))
                                  .Where(l => l.Name.Replace("lbl", "txt") == textBox.Name)
                                  .FirstOrDefault();
    
                if (label != null)
                {
                    label.FontWeight = fontWeight;
                }
            }
        }
    }
    

    XAML

        <StackPanel >
            <StackPanel.Resources>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="l:FontWeightFocusedHelper.ChangeFontWeightOnTextBoxFocused" Value="True" />
                </Style>
                <Style TargetType="{x:Type Label}">
                    <Setter Property="l:FontWeightFocusedHelper.ChangeFontWeightOnTextBoxFocused" Value="True" />
                </Style>
            </StackPanel.Resources>
            <StackPanel Orientation="Horizontal">                
                <Label x:Name="lblOne" VerticalAlignment="Center" Content="First Name"/>
                <TextBox x:Name="txtOne" Width="300" VerticalAlignment="Center"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label x:Name="lblTwo" VerticalAlignment="Center" Content="Last Name" />
                <TextBox x:Name="txtTwo" Width="300" VerticalAlignment="Center" />
            </StackPanel>
        </StackPanel>
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying change an input mask for textbox when the the check box has
I am trying to style some form labels by selecting them with their 'for'
Im trying to change images on click similar to what SO does with the
I´m trying to change a spring jsp example to use freemarker. I changed all
I'm trying to change the background color of a single subplot in a MATLAB
I'm trying to change the background color of single cell be based on a
Basically I'm trying to change the Canvas.Left property of an Ellipse Silverlight control in
I'm trying to change default firstDayOfWeek for java.util.Calendar from SUNDAY to MONDAY. Is it
I'm trying to change user input in wildcard form (*word*) to a regular expression
I am trying to change the rows output by PHP in a table to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.