Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fade out a data bound text block when the property it is bound to is changed, using MVVM

i am using the MVVM design pattern and do not want much code in my code behind. coding in XAML and C#.

when a user saves a new record i would like "record saved" to appear in a text Block then fade away.

this is the sort of thing i would like to work:

<TextBlock Name="WorkflowCreated" Text="Record saved">
  <TextBlock.Triggers>
    <DataTrigger Binding="{Binding Path=NewWorkflowCreated}">
       <DataTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                 Storyboard.TargetName="WorkflowCreated" 
                 Storyboard.TargetProperty="(TextBlock.Opacity)"
                 From="1.0" To="0.0" Duration="0:0:3"/>
            </Storyboard>
        </BeginStoryboard>
     </DataTrigger.EnterActions>
  </DataTrigger>
</TextBlock.Triggers>

so when NewWorkflowCreated is changed in the viewmodel it would trigger the animation, unfortunately this does not work. i have also tried this:

<TextBlock Name="Message" Text="This is a test.">
 <TextBlock.Triggers>
  <EventTrigger RoutedEvent="TextBlock.Loaded">
   <BeginStoryboard>
    <Storyboard>
      <DoubleAnimation
        Storyboard.TargetName="Message" 
        Storyboard.TargetProperty="(TextBlock.Opacity)"
        From="1.0" To="0.0" Duration="0:0:3"/>
    </Storyboard>
   </BeginStoryboard>
  </EventTrigger>
 </TextBlock.Triggers>
</TextBlock>

any help would be much appreciated. Maybe there is away that requires code in the View model?

like image 239
Robbie Torrens Avatar asked Dec 30 '25 22:12

Robbie Torrens


1 Answers

You're using a DataTrigger which needs to be in a style.

<Window.DataContext>
    <WpfApplication2:TestViewModel/>
</Window.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.Resources>
        <Style x:Key="textBoxStyle" TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=NewWorkflowCreated}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="1.0" To="0.0" Duration="0:0:3"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <TextBlock Name="WorkflowCreated" Style="{StaticResource textBoxStyle}" Text="Record saved" />
    <Button Content="press me" Grid.Row="1" Click="Button_Click_1"/>
</Grid>

public class TestViewModel : INotifyPropertyChanged
{
    private bool _newWorkflowCreated;
    public bool NewWorkflowCreated
    {
        get { return _newWorkflowCreated; }
        set { 
            _newWorkflowCreated = value;
            PropertyChanged(this, new PropertyChangedEventArgs("NewWorkflowCreated"));
        }
    }


    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}
like image 53
Phil Avatar answered Jan 02 '26 16:01

Phil