c# - 文本 block 边距导致文本越界

标签 c# xaml uwp marquee

我目前正在尝试创建一个具有滚动文本(从左到右和从右到左)的可视化组件 - 几乎是一个 html 选取框。

我有一个分成几列和几行的网格,我想将我的组件放在其中一个网格槽中。

网格(名为 UIGrid)是这样生成的:

for (int i = 0; i < xDivisions; i++)
{
    ColumnDefinition newColumn = new ColumnDefinition();
    UIGrid.ColumnDefinitions.Add(newColumn);
}
for (int i = 0; i < yDivisions; i++)
{
    RowDefinition newRow = new RowDefinition();
    UIGrid.RowDefinitions.Add(newRow);
}

我要添加的组件只是一个带有子文本 block 的边框。我像这样将边框放在网格内:

border = new Border();
Grid.SetColumn(border, xPosition);
Grid.SetRow(border, yPosition);

textBlock = new TextBlock();
border.Child = textBlock;
textBlock.Text = "Scrolling text from left to right";

UIGrid.Children.Add(border);

我正在使用计时器来增加文本 block 边距,这是计时器回调的简化主体:

textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double textWidth = textBlock.DesiredSize.Width;
double visibleWidth = componentBase.ActualWidth;
double targetMargin = textWidth < visibleWidth ? visibleWidth : textWidth;

if (margin.Left == targetMargin)
{
    margin.Left = -textWidth;
} else
{
    margin.Left++;
}

当文本从左向右滑动时,它的表现很好: https://s10.postimg.org/p0nt7vl09/text_good.png “离开”网格槽的文本被隐藏。

但是,当我将文本 block 的边距设置为负数时,它可能会从左侧返回到可视区域内,即使文本在分配的插槽之外,它也是可见的: https://s10.postimg.org/pownqtjq1/text_bad.png

我试过使用内边距代替,但我无法设置负内边距。我尝试了一些其他的东西,但我觉得我遇到了障碍。

我该怎么做才能获得漂亮的滚动文本?

最佳答案

如果你想要很好地滚动文本,ListView 可能是更好的选择。它是动态的,您可以将它绑定(bind)到您的对象。这需要大量的猜测。

关于c# - 文本 block 边距导致文本越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38898569/

相关文章:

c++ - Microsoft 在 VS C++ 2015 中用于 Windows UWP 调试的符号文件不起作用?

jenkins - 在命令行中构建 UWP 应用程序(使用 MSBUILD)

c# - Azure Redis Messagepack-csharp 内存不足异常

c# - Page.ClientScript.RegisterStartupScript 不工作

wpf - 在 WPF 资源字典中使用制表符和回车符

c# - 回收 ListView 中这个看不见的、吞噬性能的单元格是什么?

UWP 在导航 View 中使用 Acrylic

c# - 重写 ASP.Net 5 中 Azure Web App 的 config.json 文件中的配置值

c# - 如何: seaching XML child nodes

c# - 无论属性值如何,WPF 动画都会触发