Wpf布尔条件上的Marquee文本

时间:2017-07-25 06:28:43

标签: c# wpf storyboard

我使用this question提供的解决方案作为我希望其工作方式的基础

我同时实现了NegatingConverter类和显示的故事板,但与解决方案中的不同,我需要的不同之处在于,如果布尔条件为真,它只应在屏幕上滚动。

它的外观如下:

<StackPanel x:Name="favorite">
    <StackPanel.Resources>
        <Models:NegatingConverter x:Key="NegatingConverter" />
        <Storyboard x:Key="slide">
            <DoubleAnimation 
                From="0" 
                To="{Binding Width, ElementName=canvas, Converter={StaticResource NegatingConverter}}" 
                Duration="00:00:10"
                Storyboard.TargetProperty="X"  
                Storyboard.TargetName="transferCurreny"                                
                RepeatBehavior="Forever"/>
        </Storyboard>
    </StackPanel.Resources>
    <StackPanel.RenderTransform>
        <TranslateTransform x:Name="transferCurreny" X="0"/>
    </StackPanel.RenderTransform>
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource slide}" />
                    </DataTrigger.EnterActions>
                </DataTrigger> <!-- EXCEPTION ! -->
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <Canvas x:Name="canvas" Width="{Binding ActualWidth, ElementName=favorite}">
        <TextBlock 
                x:Name="txtMarquee" 
                Canvas.Left="0"
                Grid.Column="1"
                HorizontalAlignment="left"
                Text="{Binding Path=FavoriteInfo.Name}"
                TextTrimming="CharacterEllipsis" 
                VerticalAlignment="Center" 
                LineHeight="8" 
                MaxHeight="50"
                Foreground="Black"
                FontSize="22"/>
        <TextBlock Text="{Binding Text, ElementName=txtMarquee}" FontSize="22" Canvas.Left="{Binding Width, ElementName=canvas}"/>
    </Canvas>
</StackPanel>

但它不起作用。它在标记点抛出异常:&#34; 设置属性&#39; System.Windows.FrameworkElement.Style&#39;时触发了异常。 ...&#34;

我做错了什么?

编辑:下载详细的异常消息

    System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  LineNumber=34
  LinePosition=22
  Message='Der blev udløst en undtagelse under indstilling af egenskaben 'System.Windows.ResourceDictionary.DeferrableContent'.' Linjenummer '34' og linjeposition '22'.
  Source=PresentationFramework
  StackTrace:
       ved System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       ved System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       ved System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       ved System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       ved Spc.Modules.Browser.Views.FavoriteListView.InitializeComponent() i C:\Source\dukaPC\Source\Browser\Views\FavoriteListView.xaml:linje 1
  InnerException: 
       HResult=-2146233079
       Message=Dette Storyboard-tidslinjetræ kan ikke fryses til brug på tværs af tråde.
       Source=PresentationFramework
       StackTrace:
            ved System.Windows.Media.Animation.BeginStoryboard.Seal()
            ved System.Windows.TriggerAction.Seal(TriggerBase containingTrigger)
            ved System.Windows.TriggerActionCollection.Seal(TriggerBase containingTrigger)
            ved System.Windows.TriggerBase.Seal()
            ved System.Windows.DataTrigger.Seal()
            ved System.Windows.TriggerCollection.Seal()
            ved System.Windows.Style.Seal()
            ved System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
            ved System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            ved System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            ved System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            ved System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
            ved System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
            ved System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
            ved System.Windows.FrameworkElement.UpdateStyleProperty()
            ved System.Windows.TreeWalkHelper.OnResourcesChanged(DependencyObject d, ResourcesChangeInfo info, Boolean raiseResourceChangedEvent)
            ved System.Windows.TreeWalkHelper.InvalidateOnResourcesChange(FrameworkElement fe, FrameworkContentElement fce, ResourcesChangeInfo info)
            ved System.Windows.ResourceDictionary.NotifyOwners(ResourcesChangeInfo info)
            ved System.Windows.ResourceDictionary.SetKeys(IList`1 keyCollection, IServiceProvider serviceProvider)
            ved System.Windows.ResourceDictionary.SetDeferrableContent(DeferrableContent deferrableContent)
            ved System.Windows.Baml2006.WpfSharedBamlSchemaContext.<>c.<Create_BamlProperty_ResourceDictionary_DeferrableContent>b__297_0(Object target, Object value)
            ved System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
            ved MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
            ved MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
       InnerException: 

0 个答案:

没有答案