Reputation: 359
I'm trying to create a grid that deals with screens of different sizes. My code currently looks like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" ... />
<TextBlock Grid.Column="1" ... />
</Grid>
This doesn't work correctly because I end up with a huge empty space at the end of the first column when I'm in fullscreen, and I want the text in each column to be adjacent. However, it DOES correctly shrink the first column when I shrink/resize the screen.
I also tried using code like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" ... />
<TextBlock Grid.Column="1" ... />
</Grid>
This code looks perfect when the app is fullscreen, but when I shrink/resize the screen the first column will NOT shrink to fit. It leaves my grid the same size, which pushes my text off of the smaller screen.
How can I cap the width at whatever size 'Auto' would be, and still have it resize like it is '*'?
Upvotes: 1
Views: 1558
Reputation: 10627
How can I cap the width at whatever size 'Auto' would be, and still have it resize like it is '*'?
According to Define page layouts with XAML, Auto
means the column will size to fit its content, *
means after the Auto columns are calculated, the column gets part of the remaining width.
Width
of ColumnDefinition
cannot be set to Auto
as well as *
. But as your issue is when set Auto
to both ColumnDefinition
, it pushes your text off of the smaller screen, so here we may use AdaptiveTrigger
. We can set Width
to *
by default this can ensure resize don't cut off the text. When the window resize to a larger size that will not lead the text cut off, the AdaptiveTrigger
can help change the Width
property from *
to Auto
. For example:
<Grid
Background="Azure"
BorderBrush="Pink"
BorderThickness="2">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="col1" Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Foreground="Blue"
Text="layouttest1layouttest1layouttest1layou"
TextWrapping="Wrap" />
<TextBlock
Grid.Column="1"
Foreground="Red"
Text="layouttest2layouttest2layouttest2layouttest2layout"
TextWrapping="Wrap" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="600" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="col1.Width" Value="Auto" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
The value of MinWindowWidth
depend on the total Width
of TextBlock
. More details please reference the official sample.
Updated code as follows, if Width
of TextBlock
is dynamic.
XAML code
<Grid
Background="Azure"
BorderBrush="Pink"
BorderThickness="2">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="col1" Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
x:Name="txt1"
Foreground="Blue"
Text="layouttest1layouttest1layouttest1layou"
TextWrapping="Wrap" />
<TextBlock
Grid.Column="1"
Foreground="Red"
x:Name="txt2"
Text="layouttest2layouttest2layouttest2layouttest2layout"
TextWrapping="Wrap" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger x:Name="adptivetrigger" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="col1.Width" Value="Auto" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
Code behind
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var testwidth = txt1.ActualWidth + txt2.ActualWidth;
adptivetrigger.MinWindowWidth = testwidth;
}
Upvotes: 1
Reputation: 110
Can you try this one
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" ... HorizontalAlignment="Stretch"/>
<TextBlock Grid.Column="1" ... HorizontalAlignment="Stretch"/>
Here, I just added HorizontalAlignment="Stretch" property that will align textblock stretch within the column
Upvotes: 0