【问题标题】:using storyboard to raise a custom command. Error: Storyboard Not freezable使用情节提要引发自定义命令。错误:情节提要不可冻结
【发布时间】:2012-02-13 13:30:07
【问题描述】:

我遇到了一个故事板抱怨无法冻结的问题。谷歌上有很多关于这个的链接,但是我不确定从阅读这些信息中我如何能实现我想要的。 (即几乎只是从 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();
    }
}

我的 GestureState 枚举看起来像:

public enum GestureState
{
    Finished,
    BeingConstructed
}

我的手势控制器/命令如下所示:

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

我的视图模型看起来像:

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

目前的理解:

我从阅读中了解到,情节提要需要可冻结,以便在未冻结的线程之间快速访问。

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

【问题讨论】:

    标签: c# wpf mvvm storyboard freezable


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。我已经尝试过了,但随后发生的事情是我必须更新一个 UI 元素(我想更新我的视图模型)。当我更新 UI 元素样式(希望将其绑定回我的视图模型)时,该样式仅在鼠标悬停在列表项上时才会持续。我读过这是由于这里提到的样式的精确性:msdn.microsoft.com/en-us/library/ms743230.aspx
    • 可能我可以看看这个:geekswithblogs.net/mrnat/archive/2007/09/20/115472.aspx。基本上将您建议的内容与附加属性结合使用,我可以将其附加到 ListViewItem。然后我可以将它数据绑定到一个视图模型,我可以将该属性视为 IsSelected。
    猜你喜欢
    • 2011-12-08
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2014-11-16
    • 1970-01-01
    • 2012-07-22
    • 2015-04-14
    相关资源
    最近更新 更多