TextBox 不会在 UWP 中水平拉伸
TextBox does not stretch horizontally in 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>
我已经尝试了 HorizontalAlignment="Stretch" 和 HorizontalContentAlignment="Stretch" 但没有成功 work.Please 帮我解决这个问题
使用网格而不是相关面板对我有用
<Grid Margin="10,10,20,10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox BorderThickness="0,0,0,0" BorderBrush="#8888" HorizontalContentAlignment="Stretch" KeyDown="Box_KeyDown" 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" Grid.Row="1"/>
</Grid>
但我仍然无法弄清楚为什么它不适用于相关面板,有人请post使用相关面板的答案。
我认为,这是由于缺少精确的对齐说明,因为相对面板有点保守,无法最大限度地减少内部元素与其布局需求之间的潜在冲突。那么,您能否尝试显式设置左右对齐方式,如下所示:
... RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" ...
更新:是的,在您的情况下,布局似乎可以通过使用 Grid 元素来简化,因为内部控件太少(只有两个),因此放置它们不是问题。
蓝色部分是我的文本框,红色部分是我的相关面板。相关面板放在列表视图中
<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>
我已经尝试了 HorizontalAlignment="Stretch" 和 HorizontalContentAlignment="Stretch" 但没有成功 work.Please 帮我解决这个问题
使用网格而不是相关面板对我有用
<Grid Margin="10,10,20,10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox BorderThickness="0,0,0,0" BorderBrush="#8888" HorizontalContentAlignment="Stretch" KeyDown="Box_KeyDown" 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" Grid.Row="1"/>
</Grid>
但我仍然无法弄清楚为什么它不适用于相关面板,有人请post使用相关面板的答案。
我认为,这是由于缺少精确的对齐说明,因为相对面板有点保守,无法最大限度地减少内部元素与其布局需求之间的潜在冲突。那么,您能否尝试显式设置左右对齐方式,如下所示:
... RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" ...
更新:是的,在您的情况下,布局似乎可以通过使用 Grid 元素来简化,因为内部控件太少(只有两个),因此放置它们不是问题。