在 Silverlight 中限制 TextBlock 的宽度
明显的 MaxWidth 被忽略,并且“DisplayBox”TextBlock 中的文本显示整个文本,即使该文本继续经过父容器控件(到达 silverlight 区域的边缘)。
<win:HierarchicalDataTemplate x:Key="hierarchicalTemplate" ItemsSource="{Binding _children}">
<Border BorderThickness="0" BorderBrush="Orange" HorizontalAlignment="Stretch" Background="{Binding Converter={StaticResource BackgroundConverter}}">
<toolkit:DockPanel LastChildFill="True" Width="{Binding HeirarchyLevel, Converter={StaticResource WidthConverter}}" Height="20">
<Canvas toolkit:DockPanel.Dock="Right" Width="20" MouseLeftButtonUp="Arrow_MouseLeftButtonDown">
<Rectangle Width="20" Height="20" Fill="Transparent" />
<Line Stroke="Black" X1="5" Y1="10" X2="17" Y2="10" />
<Line Stroke="Black" X1="11" Y1="5" X2="17" Y2="10" />
<Line Stroke="Black" X1="11" Y1="15" X2="17" Y2="10" />
</Canvas>
<Ellipse Canvas.Top="5" Width="10" Height="10" Fill="Green" toolkit:DockPanel.Dock="Right" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" />
<Canvas Width="Auto" Loaded="TextArea_Loaded">
<TextBlock Name="DisplayBox" FontFamily="Arial" FontSize="17" Foreground="Black" Width="Auto" Text="{Binding TaskName}" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp" />
<TextBox Name="EditBox" FontFamily="Arial" FontSize="10" Foreground="Black" Height="20" Text="{Binding TaskName}" Visibility="Collapsed" LostFocus="TextBox_LostFocus" />
<Line Stroke="Black" X1="0" Y1="10" X2="202" Y2="10" Width="Auto" />
</Canvas>
</toolkit:DockPanel>
</Border>
</win:HierarchicalDataTemplate>
The obvious MaxWidth gets ignored and the text in the "DisplayBox" TextBlock displays the whole text even if this text continues past the parent container controls (to the edge of the silverlight area.
<win:HierarchicalDataTemplate x:Key="hierarchicalTemplate" ItemsSource="{Binding _children}">
<Border BorderThickness="0" BorderBrush="Orange" HorizontalAlignment="Stretch" Background="{Binding Converter={StaticResource BackgroundConverter}}">
<toolkit:DockPanel LastChildFill="True" Width="{Binding HeirarchyLevel, Converter={StaticResource WidthConverter}}" Height="20">
<Canvas toolkit:DockPanel.Dock="Right" Width="20" MouseLeftButtonUp="Arrow_MouseLeftButtonDown">
<Rectangle Width="20" Height="20" Fill="Transparent" />
<Line Stroke="Black" X1="5" Y1="10" X2="17" Y2="10" />
<Line Stroke="Black" X1="11" Y1="5" X2="17" Y2="10" />
<Line Stroke="Black" X1="11" Y1="15" X2="17" Y2="10" />
</Canvas>
<Ellipse Canvas.Top="5" Width="10" Height="10" Fill="Green" toolkit:DockPanel.Dock="Right" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" />
<Canvas Width="Auto" Loaded="TextArea_Loaded">
<TextBlock Name="DisplayBox" FontFamily="Arial" FontSize="17" Foreground="Black" Width="Auto" Text="{Binding TaskName}" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp" />
<TextBox Name="EditBox" FontFamily="Arial" FontSize="10" Foreground="Black" Height="20" Text="{Binding TaskName}" Visibility="Collapsed" LostFocus="TextBox_LostFocus" />
<Line Stroke="Black" X1="0" Y1="10" X2="202" Y2="10" Width="Auto" />
</Canvas>
</toolkit:DockPanel>
</Border>
</win:HierarchicalDataTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以改用网格而不是画布吗?当 TextBlock 是 Grid 的子级时,它会被适当地剪裁。 Canvas 的情况并非如此。
Can you switch to using a Grid instead of a canvas? A TextBlock will be appropriately clipped when it is a child of a Grid. This is not true of Canvas.
应该也能工作。 (边框将遵循标签的最大长度。并且边框本身将不可见,因为其厚度为 0。
Should also work. (Border will honor label's maxlength. and border itself will not be visible since its thickness is 0.