Silverlight 2 TextBlock 忽略 MaxWidth

发布于 2024-09-11 13:13:02 字数 616 浏览 6 评论 0原文

我正在使用 Silverlight 2 将 TextBlock 动态添加到 Canvas。我设置了 TextBlock 的 MaxWidth,但它忽略该值并显示比 MaxWidth 值长的字符串。

TextBlock label=new TextBlock();
label.SetValue(Canvas.LeftProperty,Convert.ToDouble(x+3));
label.SetValue(Canvas.TopProperty, Convert.ToDouble(y + 1));
label.Width = DisplayWidth - 6;
label.Height = DisplayHeight - 2;
label.TextWrapping = TextWrapping.NoWrap;
label.MaxWidth = DisplayWidth-6;
label.MinWidth = DisplayWidth-6;
label.Text = this.Title;
label.Margin = new Thickness(3.0);
baseCanvas.Children.Add(label);

我需要做什么才能让 TextBlock 将其宽度限制为特定值?理想情况下,我也会添加条件省略号(即......)。

I am using Silverlight 2 to dynamically add a TextBlock to a Canvas. I set the MaxWidth of the TextBlock but it ignores this value and displays a string longer than the MaxWidth value.

TextBlock label=new TextBlock();
label.SetValue(Canvas.LeftProperty,Convert.ToDouble(x+3));
label.SetValue(Canvas.TopProperty, Convert.ToDouble(y + 1));
label.Width = DisplayWidth - 6;
label.Height = DisplayHeight - 2;
label.TextWrapping = TextWrapping.NoWrap;
label.MaxWidth = DisplayWidth-6;
label.MinWidth = DisplayWidth-6;
label.Text = this.Title;
label.Margin = new Thickness(3.0);
baseCanvas.Children.Add(label);

What do I have to do to get the TextBlock to restrict its width to a specific value? Ideally I'll add conditional ellipses too (i.e. ...).

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

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

发布评论

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

评论(1

绝不放开 2024-09-18 13:13:02

TextBlockCanvas 的直接子级时,TextBlock 上的 MaxWidth 似乎无效。我不太明白为什么会这样。然而,解决方案是将 TextBlock 放置在 Border 中:-

TextBlock label=new TextBlock(); 
label.SetValue(Canvas.LeftProperty,Convert.ToDouble(x+3)); 
label.SetValue(Canvas.TopProperty, Convert.ToDouble(y + 1)); 
label.Width = DisplayWidth - 6; 
label.Height = DisplayHeight - 2; 
label.TextWrapping = TextWrapping.NoWrap; 
label.MaxWidth = DisplayWidth-6; 
label.MinWidth = DisplayWidth-6; 
label.Text = this.Title; 
label.Margin = new Thickness(3.0); 
Border border = new Border();
border.Child = label;
baseCanvas.Children.Add(border); 

Border 将遵循 MaxWidthTextBlock 但由于没有给定厚度,因此边框本身是不可见的。

It would seem that MaxWidth on a TextBlock is ineffectual when the TextBlock is a direct child of a Canvas. I can't quite fathom why that would be so. However the solution would be to place the TextBlock in a Border:-

TextBlock label=new TextBlock(); 
label.SetValue(Canvas.LeftProperty,Convert.ToDouble(x+3)); 
label.SetValue(Canvas.TopProperty, Convert.ToDouble(y + 1)); 
label.Width = DisplayWidth - 6; 
label.Height = DisplayHeight - 2; 
label.TextWrapping = TextWrapping.NoWrap; 
label.MaxWidth = DisplayWidth-6; 
label.MinWidth = DisplayWidth-6; 
label.Text = this.Title; 
label.Margin = new Thickness(3.0); 
Border border = new Border();
border.Child = label;
baseCanvas.Children.Add(border); 

The Border will honor the MaxWidth of the TextBlock but since it is given no thickness the border itself is invisible.

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