ListView 中的 WPF 字幕文本动画

WPF Marquee Text Animation in ListView

我正在尝试在我的代码中添加选取框文本动画。我在中找到了工作代码 WPF Marquee Text Animation 但是当我尝试将它添加到我的 ItemContainerStyle 时出现错误:未定义命名空间前缀“Local”。也许有人可以帮助我。 我不确定如何在我的项目容器样式中定义 NegatingConverter。谢谢

错误代码行:

 <local:NegatingConverter x:Key="NegatingConverter" />

代码:

namespace Line1_9_WPF
{
    public class NegatingConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is double)
            {
                return -((double)value);
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is double)
            {
                return +(double)value;
            }
            return value;
        }
    }
}

Xaml:

             <ListView ItemsSource="{Binding Messages}"
                              Background="Transparent"
                      
                              BorderBrush="Transparent"
                              ItemContainerStyle="{StaticResource ChatItem}"
                              Margin="8,0,0,0"
                    Grid.Row="1"
                >
                </ListView>

物品容器样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="ListViewItem" x:Key="ChatItem">
        <Setter Property="Background"  Value="#393B40"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>

                        <StackPanel Orientation="Vertical">
                            <Label Content="{Binding Line}"
                               Foreground="White"
                              />
                        <Label Content="{Binding MessageF1}"
                               Foreground="Gray"
                              />
                        <Label Content="{Binding MessageF2}"
                               Foreground="Gray"
                              />

                        <Label Content="{Binding Time}"
                               Foreground="Gray"
                              />
                        <StackPanel Orientation="Horizontal" x:Name="stack">
                            <StackPanel.Resources>
                                <local:NegatingConverter x:Key="NegatingConverter" />
                                <Storyboard x:Key="slide">
                                    <DoubleAnimation From="0" To="{Binding Width, ElementName=canvas, Converter={StaticResource NegatingConverter}}" Duration="00:00:03"
                      Storyboard.TargetProperty="X"
                      Storyboard.TargetName="transferCurreny"
                      RepeatBehavior="Forever"/>
                                </Storyboard>
                            </StackPanel.Resources>
                            <StackPanel.RenderTransform>
                                <TranslateTransform x:Name="transferCurreny" X="0"/>
                            </StackPanel.RenderTransform>
                            <StackPanel.Triggers>
                                <EventTrigger RoutedEvent="StackPanel.Loaded">
                                    <BeginStoryboard Storyboard="{StaticResource slide}" />
                                </EventTrigger>
                                <EventTrigger RoutedEvent="StackPanel.SizeChanged">
                                    <BeginStoryboard Storyboard="{StaticResource slide}" />
                                </EventTrigger>
                            </StackPanel.Triggers>
                            <Canvas x:Name="canvas" Width="{Binding ActualWidth, ElementName=stack}">
                                <TextBlock Text="Whosebug" FontSize="25"  x:Name="txtKron" Canvas.Left="0"/>
                                <TextBlock Text="{Binding Text, ElementName=txtKron}" FontSize="25" Canvas.Left="{Binding Width, ElementName=canvas}"/>
                            </Canvas>
                        </StackPanel>
                    </StackPanel>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
</ResourceDictionary>

您没有在 xaml

中声明转换器的命名空间

改变

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Line1_9_WPF">