滚动文本未完全显示在textBlock WPF中

时间:2019-11-21 12:42:31

标签: c# wpf xaml text scroll

因此,我对WPF及其XAML设计元素还很陌生。我想要实现的是具有无限重复的滚动文本。请注意,我是从在线资源中获取文本的,可能会很长。问题是,显示的文本恰好适合元素,在我的情况下为Width =“ 1080”。为了达到此效果,我需要在XAML或代码中进行哪些修改?这是我的代码和XAML。

<Border x:Name="FooterBorder" BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Right" Height="67" VerticalAlignment="Bottom" Width="1080" Background="#FFDE3E3E" Margin="0">
       <TextBlock Grid.Row="2" FontSize="22" Name="txtScrolling" HorizontalAlignment="Right" Width="1080">
            <TextBlock.RenderTransform>
                 <TranslateTransform x:Name="translate" />
            </TextBlock.RenderTransform>
            <TextBlock.Triggers>
                 <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                      <BeginStoryboard>
                          <Storyboard RepeatBehavior="Forever">
                               <DoubleAnimation
                                        From="1080" To="-1080"
                                        Storyboard.TargetName="translate"
                                        Storyboard.TargetProperty="X"
                                        Duration="0:0:15" />
                           </Storyboard>
                        </BeginStoryboard>
                 </EventTrigger>
            </TextBlock.Triggers>
            <Label x:Name="BottomScrollingText" Content="Custom text for scrolling test, this is just an example of how big text can't with within a certain width" Foreground="White" FontSize="36" Margin="0,0,0,0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Padding="0"/>
       </TextBlock>
</Border>

这是从在线资源获取文本的代码

public void InitScrollingText()
{
     WebClient client = new WebClient();
     Stream stream = client.OpenRead("http://touch.hola.rs/wp-content/uploads/2019/11/sample-scrolling-text.txt");
     StreamReader reader = new StreamReader(stream);
     String content = reader.ReadToEnd();
     BottomScrollingText.Content = content;
}

这是图像的链接,因为没有足够的代表我就无法发布图像。 https://prnt.sc/q02ted

如您所见,红色文本区域已满,但文本要长得多,因此无法完全滚动,为了适应元素的宽度,部分文本被切掉了。

1 个答案:

答案 0 :(得分:0)

更新,正如我在其他类似问题上发现的那样,问题出在使用XAML元素,如果内容超出其父ActualWidth,则该内容不允许显示。这是可以正确解决此问题的XAML。

<StackPanel Orientation="Horizontal" x:Name="stack" Grid.Column="0" Margin="0">
                <StackPanel.Resources>
                    <local:NegatingConverter x:Key="NegatingConverter" />
                    <Storyboard x:Key="slide">
                        <DoubleAnimation From="{Binding ActualWidth, ElementName=stack}" 
                                 To="{Binding ActualWidth, Converter={StaticResource NegatingConverter}, ElementName=TextToScroll}" 
                                 Duration="00:00:30"
                                 Storyboard.TargetProperty="X"
                                 Storyboard.TargetName="transferCurreny2"
                                 RepeatBehavior="Forever"/>
                    </Storyboard>
                </StackPanel.Resources>
                <Label Content="something" x:Name="TextToScroll" Canvas.Left="0" Foreground="#E9D460" Padding="0" Margin="0" VerticalAlignment="Center" FontSize="36">
                    <Label.Triggers>
                        <EventTrigger RoutedEvent="Label.Loaded">
                            <BeginStoryboard Storyboard="{StaticResource slide}"/>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Label.SizeChanged">
                            <BeginStoryboard Storyboard="{StaticResource slide}"/>
                        </EventTrigger>
                    </Label.Triggers>
                    <Label.RenderTransform>
                        <TranslateTransform x:Name="transferCurreny2" X="0"/>
                    </Label.RenderTransform>
                </Label>
            </StackPanel>

编辑:这是NegatingConverter的代码

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;
    }
}