You just need to make a couple of changes. First, you need the WrapPanel
to not stretch across the full Width
. Then you can data bind the TextBlock.Width
property to the WrapPanel.ActualWidth
property. Finally, you'll also need to set the HorizontalAlignment
property to Left
on the TextBlock
as well. This should do the trick:
<Grid>
<DockPanel>
<TextBlock DockPanel.Dock="Top" HorizontalAlignment="Left"
Width="{Binding ActualWidth, ElementName=Panel}" Text="Here's some really
long text that should not force the window to expand..." />
<WrapPanel Name="Panel" Orientation="Vertical" HorizontalAlignment="Left">
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
<Button>Button 4</Button>
<Button>Button 5</Button>
<Button>Button 6</Button>
<Button>Button 7</Button>
<Button>Button 8</Button>
<Button>Button 9</Button>
<Button>Button 10</Button>
</WrapPanel>
</DockPanel>
</Grid>
A final note is that it would look better if you used the TextTrimming
property to add an ellipsis (...) at the end of the text just before it is cut off. You could do that like this:
<TextBlock DockPanel.Dock="Top" HorizontalAlignment="Left" Width="{Binding ActualWidth,
ElementName=Panel}" Text="Here's some really long text that should not force the
window to expand..." TextTrimming="CharacterEllipsis" />