开发者

Limiting the width of a TextBlock in Silverlight

开发者 https://www.devze.com 2023-01-08 18:30 出处:网络
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 ar

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="1开发者_开发知识库7" 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>


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.


       <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.

0

精彩评论

暂无评论...
验证码 换一张
取 消