0

I have made an animation in a xaml.cs file:

((Storyboard)FindResource("animate")).Begin(CurrentMatchLBLProgress);

But if I copy this and take it to the ViewModel, I get an error:

The name 'FindResource' does not exist in the current context.

How do I make this line of code work from the ViewModel?

Draken
  • 3,134
  • 13
  • 34
  • 54
Rok Potočnik
  • 137
  • 13
  • Like any other "does not exist" error you miss namespace or instance of type which method you are calling. Why do you want to use `StoryBoard` in ViewModel? There should be a way without having view related stuff in the ViewModel. – Sinatr Aug 29 '18 at 11:35
  • Im doing this because I add that storyboard to specific Label. And that labels animation will be eventually turned off and animation on nev label will be started. Problem is that te labels are dynamically created, so I can't (or at least I don't know how) write all of the possibilities. – Rok Potočnik Aug 29 '18 at 11:38
  • You can start animation using data trigger (e.g. look [here](https://stackoverflow.com/a/1735810/1997232)). The ViewModel has to simply set `bool` value. – Sinatr Aug 29 '18 at 11:42

1 Answers1

1

You can access resources like below,

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml"
    >
    <Application.Resources>
        <Image x:Key="ApplicationResource" Source="ApplicationResource.bmp" />
    </Application.Resources>
</Application>


  object resource = Application.Current.TryFindResource("ApplicationResource");
WPFUser
  • 1,145
  • 7
  • 24