Wpf 文本块don'中的水平对齐;行不通
我在xaml中有这样的代码:Wpf 文本块don'中的水平对齐;行不通,wpf,xaml,listbox,Wpf,Xaml,Listbox,我在xaml中有这样的代码: <ListBoxItem> <StackPanel Orientation="Horizontal"> <TextBlock Text="{DynamicResource Referee}" Width="120" FontWeight="Bold" Margin="0 0 5 0" Horiz
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{DynamicResource Referee}" Width="120" FontWeight="Bold" Margin="0 0 5 0" HorizontalAlignment="Right"></TextBlock>
<TextBlock Text="LOL" Margin="0 0 5 0"></TextBlock>
</StackPanel>
</ListBoxItem>
问题是第一个文本块并没有对齐到右边,但仍然是向左对齐
有许多列表框项目,我想让第一列向右对齐,第二列向左对齐。在每个stackPanel中,第一个文本块的宽度为120。列表框位于列宽度设置为“自动”的网格列中
如何解决该问题?将StackPanel的属性“HorizontalAlignment”设置为“Stretch”。设置TextAlignment属性
<TextBlock Text="{DynamicResource Referee}" Width="120" FontWeight="Bold" Margin="0 0 5 0" TextAlignment="Right"></TextBlock>
默认情况下,堆栈面板从左到右堆叠项目(可通过设置FlowDirection=“RightToLeft”进行更改),因此无论堆栈面板是否跨越整个可用区域,第一个项目始终停靠在左侧,无论您如何对齐
<ListBoxItem>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Referee}" Width="120" FontWeight="Bold" Margin="0 0 5 0"></TextBlock>
<TextBlock Text="LOL" Margin="0 0 5 0"></TextBlock>
</StackPanel>
</ListBoxItem>
但是,如果希望一个文本块停靠在左侧,另一个停靠在右侧,则应使用DockPanel或网格
使用DockPanel:
<ListBoxItem>
<DockPanel>
<TextBlock Text="{DynamicResource Referee}" Width="120" FontWeight="Bold" Margin="0 0 5 0" DockPanel.Dock="Right"></TextBlock>
<TextBlock Text="LOL" Margin="0 0 5 0" DockPanel.Dock="Left"></TextBlock>
</DockPanel>
</ListBoxItem>
使用网格:
<ListBoxItem>
<Grid>
<TextBlock Text="{DynamicResource Referee}" Width="120" FontWeight="Bold" Margin="0 0 5 0" HorizontalAlignment="Right"></TextBlock>
<TextBlock Text="LOL" Margin="0 0 5 0" HorizontalAlignment="Left"></TextBlock>
</Grid>
</ListBoxItem>
默认情况下,网格和停靠面板都将填充其父级提供给它们的空间。如果未指定宽度/高度,则停靠面板的最后一个子面板将填充剩余的可用空间