How to put out a data-bound text block when the property it is bound to has changed using MVVM

I am using the MVVM design pattern and don't want a lot of code in my code. coding in XAML and C #.

when the user saves a new entry, I would like the "entry saved" to appear in the text block, then disappear.

here is what 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 will trigger an animation, unfortunately it won't work. I 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 the View model needs code that requires code?

+3


source to share


3 answers


You are using DataTrigger, which should be styled.



<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
}

      

+4


source


This type of UI specific behavior should definitely be handled in View

, not inViewModel



I would suggest to look into the event TextChanged

and see how to start the animation there

0


source


0


source







All Articles