WPF trace script for property changes

I have a DataTemplate that is for a class that implements INotifyPropertyChanged. Is there a way to invoke a storyboard when changing properties and another storyboard to different values ​​(in this case it is a bool)?

And is there a way to start the storyboard at startup depending on the values ​​of the class for which the data template is created?

+3
source share
2 answers

Yes you can do it.

Add a DataTrigger and bind it to the appropriate property. Here is an example:

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=MyProperty}" Value="True">
        <BeginStoryboard Storyboard="{StaticResource myStoryboard}"/>
    </DataTrigger>
</DataTemplate.Triggers>

, . , , false. DataTriggers ( ), .

, .

, - .

.

+6

, DataTrigger, ViewModel. , , , . node (.. ). Expression Blend, .

"Microsoft.Expression.Interactions"

XAML: ( node)

<Window
   xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
   xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
   x:Name="window" >

    ...

    <i:Interaction.Triggers>
      <ei:DataTrigger Binding="{Binding FlashingBackground, Mode=OneWay}" Value="ON">
        <ei:ControlStoryboardAction Storyboard="{StaticResource MyAnimation}"     
                                                ControlStoryboardOption="Play"/>
      </ei:DataTrigger>
    </i:Interaction.Triggers>

    ...
</Window>

ViewModel:

 private void TurnOnFlashingBackround()
    {
        FlashingBackground = "ON";
    }

    private string _FlashingBackround = "OFF";

    public string FlashingBackground
    {
        get { return _FlashingBackround; }

        private set
        {
            if (FlashingBackground == value)
            {
                return;
            }

            _FlashingBackround = value;
            this.OnPropertyChanged("FlashingBackground");
        }
    }

    public new event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

, Viewmodel "INotifyPropertyChanged"

+1

Source: https://habr.com/ru/post/1704448/


All Articles