0

I would like to trigger the method SelectAllText() when the textbox background color is red. How can I bind to code behind.

xaml:

 <TextBox Grid.Column="1" Grid.Row="0" Text="Text" MouseEnter="Test1MouseEnter" Background="{Binding TxtBoxcolor, Mode=OneWay}" Name="txbName">
        <TextBox.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="TextBox.Background" Value="Red">
                        <!--Trigger code behind-->
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

code behind:

public void SelectAllText()
    {
        txbName.SelectAll();
    }
CS Lim
  • 11
  • 7

2 Answers2

0

It's possible in your case to handle Changed event on the background in the code behind?

txbName.Background.Changed += Background_Changed;

and in the Background_Changed

private void Background_Changed(object sender, EventArgs e)
{
    var brush = sender as Brush;
    if(brush!=null)
    {
        if(brush == Brushes.Red)
        {
            txbName.SelectAll();
        }
    }
}
user2250152
  • 14,658
  • 4
  • 33
  • 57
0

Here's a way to do this with an attached event. It can only handle raising change events for one property per control. To raise events on value changes for multiple properties, you'd need an attached property that's a collection of some object with a property name and an event, which would be more complicated to write. This really just demonstrates the concept, but it's sufficient for the specific problem you have in front of you right now.

public static class PropertyChange
{
    public static readonly RoutedEvent PropertyChangeEvent = 
        EventManager.RegisterRoutedEvent("PropertyChangeEvent", RoutingStrategy.Bubble, 
            typeof(RoutedEventHandler), typeof(PropertyChange));

    public static void AddPropertyChangeEventHandler(DependencyObject d, RoutedEventHandler handler)
    {
        var uie = d as UIElement;
        if (uie != null)
        {
            uie.AddHandler(PropertyChange.PropertyChangeEvent, handler);
        }
    }

    public static void RemovePropertyChangeEventHandler(DependencyObject d, RoutedEventHandler handler)
    {
        var uie = d as UIElement;
        if (uie != null)
        {
            uie.RemoveHandler(PropertyChange.PropertyChangeEvent, handler);
        }
    }

    #region PropertyChange.PropertyName Attached Property
    public static String GetPropertyName(UIElement obj)
    {
        return (String)obj.GetValue(PropertyNameProperty);
    }

    public static void SetPropertyName(UIElement obj, String value)
    {
        obj.SetValue(PropertyNameProperty, value);
    }

    public static readonly DependencyProperty PropertyNameProperty =
        DependencyProperty.RegisterAttached("PropertyName", typeof(String), typeof(PropertyChange),
            new PropertyMetadata(null, PropertyName_PropertyChanged));

    private static void PropertyName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var target = d as UIElement;

        var oldProperty = e.OldValue as String;
        var newProperty = e.NewValue as String;

        if (oldProperty != null)
        {
            var dpd = DependencyPropertyDescriptor.FromName(oldProperty, d.GetType(), d.GetType());
            dpd.RemoveValueChanged(d, PropertyChangedHandler);
        }

        if (newProperty != null)
        {
            var dpd = DependencyPropertyDescriptor.FromName(newProperty, d.GetType(), d.GetType());
            dpd.AddValueChanged(d, PropertyChangedHandler);
        }
    }

    private static void PropertyChangedHandler(object component, EventArgs args)
    {
        var uie = component as UIElement;

        uie.RaiseEvent(new RoutedEventArgs(PropertyChange.PropertyChangeEvent, uie));
    }
    #endregion PropertyChange.PropertyName Attached Property
}

Demo

XAML

<TextBox 
    Width="100" 
    x:Name="TestTextBox"
    Text="Blah blah blah"
    local:PropertyChange.PropertyName="Background"
    local:PropertyChange.PropertyChangeEvent="TestTextBox_PropertyChangeEvent"
    >
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Code behind

private void TestTextBox_PropertyChangeEvent(object sender, RoutedEventArgs e)
{
    var tb = (TextBox)sender;
    var x = tb.Background as SolidColorBrush;

    //  Instead of examining the background color, I would much prefer to look directly 
    //  at the validation: What happens if you decide to change the error background color
    //  to a darker shade of red? Or a GradientBrush? A cosmetic decision like that should 
    //  not affect program behavior.  
    //
    //  But you didn't give any clues about how your validation is implemented, so that's 
    //  your problem not mine.
    if (x != null && x.Color == Colors.Red)
    {
        tb.Focus();
        tb.SelectAll();
    }
}