从样式隐藏的代码中的触发器方法



我想在文本框背景颜色为红色时触发方法 SelectAllText()。如何绑定到代码隐藏。

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>

代码隐藏:

public void SelectAllText()
    {
        txbName.SelectAll();
    }
在您

的情况下,是否可以在代码隐藏Changed后台处理事件?

txbName.Background.Changed += Background_Changed;

Background_Changed

private void Background_Changed(object sender, EventArgs e)
{
    var brush = sender as Brush;
    if(brush!=null)
    {
        if(brush == Brushes.Red)
        {
            txbName.SelectAll();
        }
    }
}

下面是使用附加事件执行此操作的一种方法。它只能处理每个控件的一个属性引发的更改事件。要在多个属性的值更改时引发事件,您需要一个附加属性,该属性是具有属性名称和event的某个对象的集合,编写起来会更复杂。这实际上只是演示了这个概念,但对于您现在面临的特定问题来说已经足够了。

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
}

演示

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>

代码隐藏

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();
    }
}

最新更新