从样式背后的代码中触发方法

时间:2017-10-24 01:47:26

标签: wpf xaml code-behind

我想在文本框背景颜色为红色时触发方法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();
    }

2 个答案:

答案 0 :(得分:0)

在您的情况下,可以在背后的代码中处理背景上的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();
        }
    }
}

答案 1 :(得分:0)

这是通过附加活动执行此操作的方法。它只能处理每个控件的一个属性的更改事件。要为多个属性的值更改引发事件,您需要一个附加属性,该属性是具有属性名称和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();
    }
}