使用故事板来引发自定义命令。错误:故事板不可冻结

时间:2012-01-18 20:11:58

标签: c# wpf mvvm storyboard freezable

我有一个故事板的问题,抱怨不可解体。谷歌上有很多关于此的链接,但我不确定从阅读这些信息如何实现我想要的。 (即几乎只是从IsMouseOver属性更改执行自定义命令)。我正在使用数据模板将我的列表视图更改为我在以下信息中提供的链接:

我的资源词典:

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

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

我的用户界面如下:

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

我的转换器看起来像:

    [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看起来像:

public enum GestureState
{
    Finished,
    BeingConstructed
}

My Gesture控制器/命令如下所示:

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

我的viewmodel看起来像:

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

我得到的例外是:

InvalidOperation:无法冻结故事板。

我的用户界面看起来像这样:

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

目前的理解:

我从阅读中了解到,故事板需要可以冻结,以便快速访问它们被解冻的线程。

我的问题是如何以可冻结的方式进行绑定或使用替代方法实现我想要的方式。我的核心问题是,我想在捕获手势时将鼠标悬停在listitem上时引发自定义命令或事件。

1 个答案:

答案 0 :(得分:1)

您不需要Storyboard。在Multidatatrigger中,使用OneWayToTarget DataBinding在ViewModel中设置属性。当触发条件满足时,将调用该属性的setter。在该setter中,您可以调用“this.AddToGesture = true”。

相关问题