如何在 xaml 中使用 Phone 的全角?
How to use full width of Phone in xaml?
我在 Grid.Row
中有 3 个控件,但我怎样才能让它们使用页面的整个宽度?
这是我的 xaml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Grid.Row="1"
Source="/Assets/image1.png"
Height="25"
Width="25" />
<TextBox Grid.Column="1"
Margin="10,0,0,0"
Text="{Binding InputText, Mode=TwoWay}"
BorderBrush="Black"
BorderThickness="2"
VerticalAlignment="Center">
</TextBox>
<Button Grid.Column="2"
BorderThickness="2"
HorizontalAlignment="Right"
Command="{Binding AddTextCommand}"
Margin="10,0,0,0">
<TextBlock x:Uid="ButtonText" />
</Button>
</Grid>
这是现在的结果:
如您所见,它是左对齐的,我怎样才能让它使用全宽?
您的代码只显示了 3 个控件(Image
、TextBox
、Button
),而您的屏幕截图显示了 4 个控件。我想顶部的全宽控件丢失了,但是回答这个问题没问题。
如果我们分解你的 XAML,你有:
- 第一列宽度自动,用图像填充。
- 第二列宽140,填充一个TextBox
- 第三列宽*(或其余的space),填充一个Button
在您放置的按钮上 HorizontalAlignment="Right"
(默认为左),其中您说:只使用必要的 space 并将控件放在右侧。如果要使用可用的全宽,则必须使用 HorizontalAlignment="Stretch"
.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Source="/Assets/image1.png"
Height="25"
Width="25" />
<TextBox Grid.Column="1"
Margin="10,0,0,0"
Text="{Binding InputText, Mode=TwoWay}"
BorderBrush="Black"
BorderThickness="2"
VerticalAlignment="Center">
</TextBox>
<Button Grid.Column="2" x:Uid="ButtonText"
BorderThickness="2"
HorizontalAlignment="Stretch"
Command="{Binding AddTextCommand}"
Margin="10,0,0,0" />
</Grid>
请注意,我还从 Button
中删除了 TextBlock
,并将 x:Uid
标签移至按钮。只需在资源中使用 ButtonText.Content
即可将按钮本地化,无需在其中放置 TextBlock
。
我在 Grid.Row
中有 3 个控件,但我怎样才能让它们使用页面的整个宽度?
这是我的 xaml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Grid.Row="1"
Source="/Assets/image1.png"
Height="25"
Width="25" />
<TextBox Grid.Column="1"
Margin="10,0,0,0"
Text="{Binding InputText, Mode=TwoWay}"
BorderBrush="Black"
BorderThickness="2"
VerticalAlignment="Center">
</TextBox>
<Button Grid.Column="2"
BorderThickness="2"
HorizontalAlignment="Right"
Command="{Binding AddTextCommand}"
Margin="10,0,0,0">
<TextBlock x:Uid="ButtonText" />
</Button>
</Grid>
这是现在的结果:
如您所见,它是左对齐的,我怎样才能让它使用全宽?
您的代码只显示了 3 个控件(Image
、TextBox
、Button
),而您的屏幕截图显示了 4 个控件。我想顶部的全宽控件丢失了,但是回答这个问题没问题。
如果我们分解你的 XAML,你有:
- 第一列宽度自动,用图像填充。
- 第二列宽140,填充一个TextBox
- 第三列宽*(或其余的space),填充一个Button
在您放置的按钮上 HorizontalAlignment="Right"
(默认为左),其中您说:只使用必要的 space 并将控件放在右侧。如果要使用可用的全宽,则必须使用 HorizontalAlignment="Stretch"
.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Source="/Assets/image1.png"
Height="25"
Width="25" />
<TextBox Grid.Column="1"
Margin="10,0,0,0"
Text="{Binding InputText, Mode=TwoWay}"
BorderBrush="Black"
BorderThickness="2"
VerticalAlignment="Center">
</TextBox>
<Button Grid.Column="2" x:Uid="ButtonText"
BorderThickness="2"
HorizontalAlignment="Stretch"
Command="{Binding AddTextCommand}"
Margin="10,0,0,0" />
</Grid>
请注意,我还从 Button
中删除了 TextBlock
,并将 x:Uid
标签移至按钮。只需在资源中使用 ButtonText.Content
即可将按钮本地化,无需在其中放置 TextBlock
。