标签内容更改时触发触发器 [英] Fire a trigger when Label Content changes

查看:84
本文介绍了标签内容更改时触发触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我刚刚开始学习WPF,现在我正在用C#和.NET Framework 4.7做我的第二个WPF应用程序。

I've just started to learn WPF and now I'm doing my second WPF application with C# and .NET Framework 4.7.

我想在Label的内容发生变化时触发动画。我的View上有多个标签,我希望将相同的动画关联到所有这些标签。

I want to fire an animation when Label's Content changes. I have more than one label on my View and I want to associate the same animation to all of them.

这是我的标签之一,但它们大部分都是相同的:

This is one of my labels, but all of them are mostly identical:

<Label x:Name="c30" Content="{Binding BestIndividual[27]}" Margin="0" Grid.Row="3" BorderBrush="Black" BorderThickness="2,2,0,0"/>

我认为是这样的:

<Style.Triggers>        <Trigger Property="IsMouseOver" Value="True">


当Label的内容发生变化时,如何触发触发器?



How can I fire a trigger when a Label's Content changes?


推荐答案

嗨VansFannel,

Hi VansFannel,

根据您的描述,您只想在标签内容更改时触发触发器,我举一个示例,您可以查看。  标签的Content属性的任何更改都是动画的。它是使用触发器和值转换器类
完成的,它将内容值转换为"True"的小技巧。或"假"和触发器设置为对这两个值做出反应。 Cnverter附加到标签的Tag属性,该属性弯曲到数据上下文的Name属性。

According to your description, you just want to fire a trigger when label content changes, I do one example that you can take a look.  Any change for Content property of the label is animated. It's done using triggers and value converter class which does the small trick converting content value to either "True" or "False" and triggers are set up to react on those 2 values. Cnverter is attached to the Tag property of the label which is bent to the Name property of data context.

转换器:

  class converter1:IValueConverter
    {
        private string _originalValue = String.Empty;
        private bool _previousValue = false;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            _originalValue = (string)value;
            _previousValue = !_previousValue;
            return _previousValue.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return _originalValue;
        }

    }

xaml:

   <Label
            Name="label1"
            Width="120"
            Height="28"
            Margin="132,96,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top">

            <Label.Content>
                <Binding Path="name" />
            </Label.Content>
            <Label.Tag>
                <Binding Converter="{StaticResource testconverter}" Path="name" />
            </Label.Tag>
            <Label.Background>
                <SolidColorBrush x:Name="animatedBrush1" Color="Yellow" />
            </Label.Background>
            <Label.Foreground>
                <SolidColorBrush x:Name="animatedBrush2" Color="Red" />
            </Label.Foreground>
            <Label.Style>
                <Style TargetType="Label">
                    <Style.Triggers>
                        <Trigger Property="Tag" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard AutoReverse="True">
                                        <!--<DoubleAnimation
                                        Storyboard.TargetProperty="FontSize" To="20"/>-->
                                        <DoubleAnimation
                                            AutoReverse="True"
                                            Storyboard.TargetProperty="Opacity"
                                            To="0.0" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                        </Trigger>
                        <Trigger Property="Tag" Value="False">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard AutoReverse="True">
                                        <DoubleAnimation Storyboard.TargetProperty="FontSize" To="20" />
                                        <!--"<DoubleAnimation
                                        Storyboard.TargetProperty="Opacity" To="0.0" AutoReverse="True"/>-->
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
           
        </Label>
        <Button
            x:Name="btn"
            Width="200"
            Height="30"
            Click="btn_Click"
            Content="click me" />




xaml.cs:

  public Window2()
        {
            InitializeComponent();
            label1.DataContext = new test1() { name = "cherry" };
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            label1.DataContext = new test1() { name = "barry" };
        }

      
    }
    public class test1
    {
        public string name { get; set; }
    }

最好的问候,

Cherry


这篇关于标签内容更改时触发触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆