在 Silverlight 中限制 TextBlock 的宽度

发布于 2024-09-10 22:30:52 字数 1803 浏览 3 评论 0原文

明显的 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 技术交流群。

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

发布评论

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

评论(2

梦里°也失望 2024-09-17 22:30:52

您可以改用网格而不是画布吗?当 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.

灵芸 2024-09-17 22:30:52
       <Canvas Width="Auto" Loaded="TextArea_Loaded">
**<Border>**                    
<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" />
**</Border>**
                </Canvas>

应该也能工作。 (边框将遵循标签的最大长度。并且边框本身将不可见,因为其厚度为 0。

       <Canvas Width="Auto" Loaded="TextArea_Loaded">
**<Border>**                    
<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" />
**</Border>**
                </Canvas>

Should also work. (Border will honor label's maxlength. and border itself will not be visible since its thickness is 0.

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