如何强制文本达到特定宽度?

发布于 2024-10-06 16:42:34 字数 1648 浏览 0 评论 0原文

我使用 GridView 来显示我的布局。对此 GridView,我手动添加一些 RowDefinitions,并在此 RowDefinitions 中添加 1 个包含 2 个矩形的画布:

 foreach (Method m in sourceFile.getMethods())
            {
                if (!m.getName().StartsWith("<") && !m.getName().EndsWith(">"))
                {
                    RowDefinition row = new RowDefinition();
                    row.Height = GridLength.Auto;
                    MethodsContainer.RowDefinitions.Add(row);

                    Canvas c = new Canvas();
                    c.Width = width;
                    c.Height = height;
                    c.Tag = m;
                    Contacts.AddPreviewContactDownHandler(c, new ContactEventHandler(onContactDown));

                    Rectangle r1 = new Rectangle();
                    r1.Height = height;
                    r1.Width = m.getLoc() * (width / 1000);
                    Canvas.SetLeft(r1, 0);
                    Canvas.SetLeft(r1, 0);
                    r1.Fill = Brushes.Red;

                    Rectangle r2 = new Rectangle();
                    r2.Height = height;
                    r2.Width = width - r1.Width;
                    Canvas.SetTop(r2, 0);
                    Canvas.SetLeft(r2, r1.Width);
                    r2.Fill = Brushes.Blue;

                    c.Children.Add(r1);
                    c.Children.Add(r2);

                    Grid.SetRow(c, rowCounter);
                    MethodsContainer.Children.Add(c);
                    rowCounter++;
                }
            }

画布宽度为 200 像素,高度为 30 像素。两个矩形都准确地填充了画布。现在我想在这两个矩形上添加一些文本。但我不知道文字有多长。不过我想强制文本始终打印到这个 200px 中。我怎样才能做到这一点?

I use a GridView to display my layout. To this GridView i manually add some RowDefinitions and in this RowDefinitions I add 1 Canvas containing 2 rectangles:

 foreach (Method m in sourceFile.getMethods())
            {
                if (!m.getName().StartsWith("<") && !m.getName().EndsWith(">"))
                {
                    RowDefinition row = new RowDefinition();
                    row.Height = GridLength.Auto;
                    MethodsContainer.RowDefinitions.Add(row);

                    Canvas c = new Canvas();
                    c.Width = width;
                    c.Height = height;
                    c.Tag = m;
                    Contacts.AddPreviewContactDownHandler(c, new ContactEventHandler(onContactDown));

                    Rectangle r1 = new Rectangle();
                    r1.Height = height;
                    r1.Width = m.getLoc() * (width / 1000);
                    Canvas.SetLeft(r1, 0);
                    Canvas.SetLeft(r1, 0);
                    r1.Fill = Brushes.Red;

                    Rectangle r2 = new Rectangle();
                    r2.Height = height;
                    r2.Width = width - r1.Width;
                    Canvas.SetTop(r2, 0);
                    Canvas.SetLeft(r2, r1.Width);
                    r2.Fill = Brushes.Blue;

                    c.Children.Add(r1);
                    c.Children.Add(r2);

                    Grid.SetRow(c, rowCounter);
                    MethodsContainer.Children.Add(c);
                    rowCounter++;
                }
            }

The canvas is 200px width and 30px height. Both rectangles fill the Canvas exactly. Now i want to add some text over this both Rectangles. But I don't know how long the text is. However I want to force that the text is always printed into this 200px. How can I achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅语花开 2024-10-13 16:42:34

听起来你可以使用 ViewBox。这将使您的文本在水平和垂直方向上拉伸。如果我正确理解了这个问题,我想这就是你想要的。 xaml 中的示例

<Canvas Width="200"
        Height="30">
    <Viewbox Width="200"
             Height="30">
        <TextBlock Text="Text that will fit in 200 Width"/>
    </Viewbox>
</Canvas>

在其后面的代码中将类似于

TextBlock textBlock = new TextBlock();
textBlock.Text = "Text that will fit in 200 Width";

Viewbox viewBox = new Viewbox();
viewBox.Width = width;
viewBox.Height = height;
viewBox.Child = textBlock;

c.Children.Add(viewBox);

Sounds like you could make use of a ViewBox. This will make your text stretch both horizontally and vertically. I assume that's what you want if I understood the question correctly. Example in xaml

<Canvas Width="200"
        Height="30">
    <Viewbox Width="200"
             Height="30">
        <TextBlock Text="Text that will fit in 200 Width"/>
    </Viewbox>
</Canvas>

And in code behind it will be like

TextBlock textBlock = new TextBlock();
textBlock.Text = "Text that will fit in 200 Width";

Viewbox viewBox = new Viewbox();
viewBox.Width = width;
viewBox.Height = height;
viewBox.Child = textBlock;

c.Children.Add(viewBox);
那一片橙海, 2024-10-13 16:42:34
var txt = new TextBlock();
txt.MaxWidth = 200;

// optionally you might want the text to wrap if too long
txt.TextWrapping = TextWrapping.Wrap;

顺便说一句,它在 WPF 中不是像素,而是与设备无关的单位(1/96 英寸)测量单位。

var txt = new TextBlock();
txt.MaxWidth = 200;

// optionally you might want the text to wrap if too long
txt.TextWrapping = TextWrapping.Wrap;

By the way, it's not pixels in WPF, it's a device-independent unit (1/96th inch) measurement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文