【问题标题】:ListView 中的 WPF 选取框文本动画
【发布时间】:2022-01-23 03:24:25
【问题描述】:

我正在尝试在我的代码中添加选取框文本动画。我在 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>

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>

【问题讨论】:

    标签: c# wpf listview marquee


    【解决方案1】:

    您没有在 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">
    

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      相关资源
      最近更新 更多