首页 > 解决方案 > TextBox 在 UWP 中不会水平拉伸

问题描述

在此处输入图像描述

蓝色部分是我的文本框,红色是我的相关面板。相关面板放置在列表视图中

<ListView RelativePanel.Below="Line" Name="SubTasksListView" Margin="10,10,10,0"   HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"   ItemsSource="{x:Bind subtasks}" IsItemClickEnabled="True" ItemClick="ItemClick"   ItemTemplate="{StaticResource SubTaskDataTemplate}"/>



 <DataTemplate x:DataType="data:ZTask" x:Key="SubTaskDataTemplate">
 <RelativePanel Margin="10,10,20,10"  HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Background="Red" >
            <TextBox Background="Aqua" BorderThickness="0,0,0,0" BorderBrush="#8888" HorizontalContentAlignment="Stretch"  KeyDown="Box_KeyDown" RelativePanel.AlignLeftWithPanel="True" Name="SubTaskTitle" PlaceholderText="+ Subtask" FontSize="16"   Margin="0"/>
            <Line Name="Line" Stretch="Fill" Margin="10 0 0 0" Stroke="#8888" X2="1" Opacity="0.2" RelativePanel.Below="SubTaskTitle"/>
</RelativePanel>
</DataTemplate>

我试过 Horizo​​ntalAlignment="Stretch" 和 Horizo​​ntalContentAlignment="Stretch" 但它不起作用。请帮我解决这个问题

标签: c#visual-studioxamluwpuwp-xaml

解决方案


我相信,这是由于缺乏精确的对齐指令,因为相对面板有点保守,以尽量减少内部元素与其布局需求之间的潜在冲突。那么,您能否尝试显式设置左右对齐方式,如下所示:

... RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" ...

Upd:是的,在您的情况下,似乎可以通过使用 Grid 元素来简化布局,因为内部控件太少(只有两个),因此定位它们不是问题。


推荐阅读