Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto clip TextBlock in WPF

Tags:

wpf

I have a TextBlock which is resizing itself depending on its Text which is causing my entire window to resize. I want the text to truncate instead - but I don't want to explicitly set the MaxWidth of the TextBlock in case the user resizes the window.

Before import button pressed:

enter image description here

Current output:

current output

Desired output:

desired output

   <Grid Grid.Row="3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Name="status" Margin="3" HorizontalAlignment="Stretch" TextTrimming="CharacterEllipsis"></TextBlock>
        <Label Name="lblPercentage" Grid.Column="1" HorizontalAlignment="Right" ></Label>
        <Button Grid.Column="2" Margin="3" Name="cmdImport" Click="import_Click" HorizontalAlignment="Right">Import</Button>
        <Button Grid.Column="3" Margin="3" Name="cmdCancel" Click="cancel_Click" HorizontalAlignment="Right">Close</Button>
    </Grid>
like image 705
Matt Frear Avatar asked May 13 '11 16:05

Matt Frear


1 Answers

Just don't set SizeToContent on your Window.

like image 135
svick Avatar answered Sep 23 '22 23:09

svick