3

So right now I'm using a property on my control that binds to a bool property on my viewmodel called searcheditemfound (with its pair, searcheditemnotfound). I can't have just one property because even if I raise OnPropertyChanged("variableName), it will not activate the trigger unless there was an actual value change. This means that in the code for the view model, I have to do this really ugly looking:

SearchedItemNotFound = false; SearchedItemNotFound = true;

private bool _SearchedItemNotFound;
public bool SearchedItemNotFound
{
  get
  {
    return _SearchedItemNotFound;
  }
  set
  {
    if (value != _SearchedItemNotFound)
    {
      _SearchedItemNotFound = value;
      OnPropertyChanged("SearchedItemNotFound");
    }
  }
}

when what I would really like is to just tie to an event in the view model. However, eventtriggers only trigger off routed events. Can I place routed events in the viewmodel? I think I have to inherit from control to do that.

One post here: How can I Have a WPF EventTrigger on a View trigger when the underlying Viewmodel dictates it should?

mentioned using

<i:Interaction.Triggers>
    <samples:DataEventTrigger EventName="YourEvent">
        <im:ControlStoryboardAction Storyboard="{StaticResource Storyboard1}" 
               ControlStoryboardOption="Play"/>
    </samples:DataEventTrigger>
</i:Interaction.Triggers>

which looks perfect, except that it requires expression blend and I'm in visual studio 2008 where it isn't available. I'm wondering what other options I have. I don't even mind if it's not MVVM. I'm not a purist, I'm just trying to understand what my options are.

Community
  • 1
  • 1
James Joshua Street
  • 3,259
  • 10
  • 42
  • 80
  • [`Here`](http://stackoverflow.com/questions/21067774/is-accessing-the-viewmodel-in-code-behind-always-violating-the-mvvm-pattern/21067879#21067879) I wrote an answer, which refers to the use of events in `ViewModel`. – Anatoliy Nikolaev Feb 11 '14 at 04:37

1 Answers1

2

A RoutedEvent is a UI element and has no place in a view model. Personally, I can't really see the problem with changing your bool property from true to false and then back to true again... there is a valid reason for doing that. If it really bothers you, then just hide it away in a method and call that instead:

private StartSearchedItemNotFoundAnimation()
{
    SearchedItemNotFound = false; 
    SearchedItemNotFound = true;
}

There's hardly any point in using any more complex and/or expensive ways to replicate the functionality that you already have.

Sheridan
  • 68,826
  • 24
  • 143
  • 183