Question

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>
Was it helpful?

Solution

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!

OTHER TIPS

You can have all gotfocus go to the same event. The sender is passed to the event so you can get the name of the sender. In code behind you can use variable and logic that is not available in XAML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top