Search code examples
c#wpflistviewmarquee

WPF Marquee Text Animation in ListView


I'm trying to add marquee text animation in my code. I found working code in WPF Marquee Text Animation But when i trying to add it to my ItemContainerStyle i got error: The namespace prefix "Local" is not defined. Maybe someone can help me. i'm not sure how to define NegatingConverter in my Item Container Style. Thx.

Error code line:

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

Code:

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>

ItemContainerStyle:

<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="StackOverflow" 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>

Solution

  • You are not declaring the namespace for the converter in the xaml

    Change

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

    To

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