0

I have a xaml view which contains an animation that I put into a content control. In the code behind for the xaml, I load the animation through storyboard find resources. This is fine so far. What I'm having issue is in my xaml there's a button which triggers a relay command from the view model, execute database calls then prompt a message. What I want to achieve is when I click on the button, I would like to show the animation and then when the message box show, i would like to hide the animation. So far no luck.

MainView

<ContentControl Name="loader" />

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Content="Test" Command="{Binding TestCommand, Mode=OneWay}" />
</StackPanel> 

MainView.cs

public MainView()
{
    InitializeComponent();

    _loading = new LoadingUC();  
    _loaderUC = _loading;         
    showLoading.Content = _loaderUC;

    Storyboard showUC = FindResource("Test_Loading") as Storyboard;
    showUC.Begin(_loaderUC);    
}

TestViewModel

public ICommand TestCommand
{
    get
    {
        return _TestCommand ?? (_TestCommand = new RelayCommand(p => TestSave()));
    }
}

private void TestSave()
{
// show loading
    if (SaveSuccessFul() == true)
    {
        //hide loading
        MessageBox.Show("Save Completed");

    }
}
Calvin
  • 631
  • 5
  • 17
  • 38

1 Answers1

0

Your ViewModel can raise an event just before showing the message box.

public event EventHandler LoadSuccessful;

private void TestSave()
{
// show loading
    if (SaveSuccessFul() == true)
    {
        //hide loading
        if (LoadSuccessful != null)
           LoadSuccessful(this, EventArgs.Empty);
        MessageBox.Show("Save Completed");

    }
}

The view can register to that event and stop the animation when the event is received.

public MainView()
{
    InitializeComponent();

    _loading = new LoadingUC();  
    _loaderUC = _loading;         
    showLoading.Content = _loaderUC;

    Storyboard showNewPatUC = FindResource("Test_Loading") as Storyboard;
    showNewPatUC.Begin(_loaderUC);  
}

void OnLoadSuccessful(object sender, EventArgs args)
{
    Storyboard showNewPatUC = FindResource("Test_Loading") as Storyboard;
    showNewPathUC.Stop();
}
Alex Gelman
  • 534
  • 3
  • 11
  • @ Alex Gelman - can you please help? – Calvin Sep 27 '12 at 17:22
  • I've added a code example. I don't know how you hook the view with the viewmodel so I omitted the code that registers to the `LoadSuccessful` event in the view. – Alex Gelman Sep 27 '12 at 17:29
  • I'm still not sure how to set up the event to make this work with my view and viewmodel. I'm hooking the view with the viewmodel like so: – Calvin Sep 27 '12 at 18:53
  • check out this answer: http://stackoverflow.com/questions/1424202/how-can-i-have-a-wpf-eventtrigger-on-a-view-trigger-when-the-underlying-viewmode/1996324#1996324 – Alex Gelman Sep 27 '12 at 19:10