In the following XAML the button will stretch to fit the width of the window, including as you resize the window. However, the TextBlock and blue box are centered. How would you change it so that:
1) the TextBlock is inside the Button, but left justified with the actual Button width (i.e. on the left side of the Window)
2) the Canvas is inside the Button, but right-justified with the actual Button width (i.e. on the right side of the Window)
It seems "HorizontalAlignment=Stretch" doesn't work in this case, and, when using Auto sizing, the Grid inside the Button only ever grows to the minimum width needed for its contents.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="test"
Width="640" Height="480">
<Grid x:Name="LayoutRoot">
<Button Height="30">
<Grid HorizontalAlignment="Stretch">
<TextBlock Text="Sample Text" HorizontalAlignment="Stretch" TextAlignment="Left"></TextBlock>
<Canvas Width="40" Background="AliceBlue" HorizontalAlignment="Right"></Canvas>
</Grid>
</Button>
</Grid>
</Window>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…