I have the following simple program
<Window x:Class="TextCutoffExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="100" Width="200">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" TextTrimming="CharacterEllipsis"/>
<Button Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Content="..."/>
</Grid>
</Window>
When run it produces this
pushing the button outside of the window. If I make the 2nd column Auto
and the 1st column *
with it looks correctly when the text is too large
However when the text is not too large making the 2nd column auto puts it at the far right.
What I want to happen is the button to be right next to the text
The closest I could get to getting this to work was setting a max width on the text block and having the 1st column set to Auto
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" TextTrimming="CharacterEllipsis"
MaxWidth="180"/>
<Button Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Content="..."/>
That gives me the behavior I want, but I don't know what the maximum width will be at compile time in the real code I will be using this in.
What do I need to do to make the button follow the width of the text but if the text becomes too large for the window it will be cut off with ellipsis without setting a fixed max width?