Search code examples
c#wpfmvvmstoryboardfreezable

using storyboard to raise a custom command. Error: Storyboard Not freezable


I have having issues with a storyboard that is complaining about being unfreezable. There are alot of links on google about this, however I am not sure from reading that information how I can achieve what I want. (ie pretty much just execute a custom command from IsMouseOver property change). I am using data templating to change my listview to look like the link I have provided in the information below:

My resource dictionary:

<DataSourceProviders:ServiceLocatorProvider ServiceType="{x:Type Interfaces:IGestureBuilderModuleController}" x:Key="GestureController"/>

<Converters:IsGestureBeingBuiltConverter x:Key="IsGestureBeingBuildConverter" />

My UI looks like:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
                <MultiDataTrigger>
                      <MultiDataTrigger.Conditions>
                          <Condition Binding="{Binding IsMouseOver}" Value="True" />
                          <Condition Binding="{Binding Path=CurrentGestureState, Converter={StaticResource IsGestureBeingBuildConverter}}" Value="True" />
                      </MultiDataTrigger.Conditions>

                      <!--TODO:Need to add a button to this control. Then use the buttons event to trigger command.-->

                      <MultiDataTrigger.ExitActions>

                            <BeginStoryboard>
                              <StoryBoard:CommandTimeline StoryBoard:CommandStoryboard.Command="{Binding Source={StaticResource ResourceKey=GestureController}, Path=AddToGestureCommand}" />
                            </BeginStoryboard>
                        </MultiDataTrigger.ExitActions>
                </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

My converter looks like:

    [ValueConversion(typeof(GestureState), typeof(bool))]
public class IsGestureBeingBuiltConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Equals(value, GestureState.BeingConstructed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

My GestureState Enum looks like:

public enum GestureState
{
    Finished,
    BeingConstructed
}

My Gesture controller/command looks like:

public class GestureBuilderModuleController : ModuleController, IGestureBuilderModuleController
{
    public ICommand AddToGestureCommand
    {
        get { return new DelegateCommand<GestureBuilderViewModel>(viewModel => viewModel.AddToGesture = true); }
    }
}

My viewmodel looks like:

public GestureableItemViewModel ItemBeingAdded { get; set; }
public virtual bool AddToGesture
{
    get { return false; }
    set
    {
        if (ItemBeingAdded == null) return;
        if(CurrentGestureState != GestureState.BeingConstructed) return;

        SelectedItems.Add(ItemBeingAdded);
    }
}

The exception I am getting is:

InvalidOperation: Cannot freeze storyboard.

My UI looks LIKE this:

http://socialbarrel.com/wp-content/uploads/2011/03/Android-Like-Gesture-Unlock-Screen-Being-Tested-By-Apple-Report.jpg?98c14d

Current understanding:

I understand from reading that storyboards need to be freezable for quick access across threads where they are unfrozen.

My Question is how can I make my binding in a way that it is freezable OR achieve what I want using an alternate approach. My core problem is that I want to raise a custom command or event when mousing over a listitem when capturing a gesture.


Solution

  • You don't need Storyboard. In your Multidatatrigger, use a OneWayToTarget DataBinding to set a property in your ViewModel. That property's setter will be invoked when the trigger condition meets. In that setter you can call "this.AddToGesture = true".