Silverlight 2 TextBlock 忽略 MaxWidth
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
TextBlock
是Canvas
的直接子级时,TextBlock
上的MaxWidth
似乎无效。我不太明白为什么会这样。然而,解决方案是将TextBlock
放置在Border
中:-Border
将遵循MaxWidth
的TextBlock
但由于没有给定厚度,因此边框本身是不可见的。It would seem that
MaxWidth
on aTextBlock
is ineffectual when theTextBlock
is a direct child of aCanvas
. I can't quite fathom why that would be so. However the solution would be to place theTextBlock
in aBorder
:-The
Border
will honor theMaxWidth
of theTextBlock
but since it is given no thickness the border itself is invisible.