简体   繁体   中英

TextTrimming on TextBlock while Text bound by DynamicResource

there is a Problem in my TextBlock. I have a Resource in a ResourceDictionary of type String and this is bound to an TextBlock via DynamicResource.

On my TextBlock there is TextTrimming active, but on this "Binding" the TextTrimming is not working.

Is this a bug in the framework or i am doing it wrong?

Here's my sample:

   <Grid Grid.Column="1" HorizontalAlignment="Center" Height="60" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
<TextBlock Text="{DynamicResource PART_HeaderString}" VerticalAlignment="Center"  TextTrimming="CharacterEllipsis" Grid.Column="1"/>
                </Grid>

and my String in the resource:

<sys:String x:Key="PART_HeaderString">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</sys:String>

There are many ways to get this working, but I want to know why this isn't working right.

You need to constraint Grid width for TextTrimming to be applied. TextBlock is place in second column whose width is set to Auto so it will expand till actual width of control ie TextBlock in your case.

Instead set some hardcoded width for second column say 50 or set it to * . So that all 3 columns share same width.

<Grid.ColumnDefinitions>
   <ColumnDefinition Width="*"/>
   <ColumnDefinition Width="50"/> <-- HERE
   <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

You will see TextTrimming gets applied now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM