繁体   English   中英

C#WPF触发器文本框从代码更新

[英]C# WPF Trigger textbox update from code

我有这个文本框:

<TextBlock x:Name="StatusTextBlock" Foreground="Black">
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard Duration="0:0:4" >
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                  Storyboard.TargetName="StatusTextBlock"
                                  AutoReverse="False">
                                    <ColorAnimationUsingKeyFrames.KeyFrames>
                                        <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Black"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="DarkGray"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Black"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:1.5" Value="DarkGray"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:2" Value="Black"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:2.5" Value="DarkGray"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:3" Value="Black"/>
                                    </ColorAnimationUsingKeyFrames.KeyFrames>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>

现在在我的代码背后,我想触发一个方法内的更新:

public void UpdateStatusBlock(string text)
    {
        StatusTextBlock.Text = text;
        // How can i trigger the animation in here
    }

如何触发动画?

为了使其工作,您必须将TextBlox.Text属性绑定到实现INotifyPropertyChanged的某些对象。 为什么? 因为您定义了仅在“ Binding.TargetUpdated”时触发的触发器。

这里看起来像:

public partial class MainWindow : Window
    {       
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new SampleViewModel();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SampleViewModel vm = (SampleViewModel) this.DataContext;
            vm.SampleText = DateTime.Now.Ticks.ToString();
        }
    }

public class SampleViewModel : INotifyPropertyChanged
    {
        private string _sampleText;

        public string SampleText
        {
            get { return _sampleText; }
            set
            {
                _sampleText = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;        
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

最后,您必须将Text属性绑定到视图模型的“ SampleText”属性。

 <TextBlock x:Name="StatusTextBlock" Foreground="Black" Text="{Binding SampleText,NotifyOnTargetUpdated=True}">

请注意,还必须定义NotifyOnTargetUpdated,因为它指示是否引发TargetUpdated事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM