【问题标题】:Trigger method in code behind from style样式代码中的触发方法
【发布时间】:2017-10-26 23:37:52
【问题描述】:

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

【问题讨论】:

  • 是什么让背景变红?
  • 点击按钮后有验证。如果该值无效,viewmodel 会将文本框颜色更改为红色。
  • viewmodel如何让背景变红?
  • 我刚刚改了代码,背景有绑定到viewmodel
  • 谢谢。您可能会查看事件触发器:stackoverflow.com/a/5761641/424129

标签: wpf xaml code-behind


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2011-09-30
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多