这应该很简单 - 我已经把头撞在桌子上很长时间,试图让一个看起来简单的任务工作(让我觉得WPF不直观或有bug)......
无论如何,我有一个设置为水平方向的Stackpanel,在其中有两个TextBlocks。我希望第二个TextBlock将其文本显示到右侧。
我该怎么做呢?
做所有这些让我想起了我为什么放弃了Silverlight。 :p
这应该很简单 - 我已经把头撞在桌子上很长时间,试图让一个看起来简单的任务工作(让我觉得WPF不直观或有bug)......
无论如何,我有一个设置为水平方向的Stackpanel,在其中有两个TextBlocks。我希望第二个TextBlock将其文本显示到右侧。
我该怎么做呢?
做所有这些让我想起了我为什么放弃了Silverlight。 :p
如果您不想像StackPanel那样堆叠所有元素,则需要使用DockPanel。为了使第二个TextBlock右对齐,您可以添加一个额外的虚拟TextBlock来填充它们之间的区域:
<DockPanel>
<TextBlock>Left text</TextBlock>
<TextBlock DockPanel.Dock="Right">Right text</TextBlock>
<TextBlock />
</DockPanel>
或者你可以使用属性: <DockPanel>
<TextBlock>Left text</TextBlock>
<TextBlock TextAlignment="Right">Right text</TextBlock>
</DockPanel>
<Grid>
<TextBlock>Left text</TextBlock>
<TextBlock TextAlignment="Right">Right text</TextBlock>
</Grid>
针对您的评论,这里提供了另一个示例,展示了实现您所需的两种方式,即网格布局和停靠面板布局。根据描述,停靠面板布局可能是您需要的。如果不行,请提供更清晰的关于所需布局和属性的描述。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.45*" />
<RowDefinition Height="0.05*" />
<RowDefinition Height="0.45*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<!-- note: you don't need to declare ColumnDefintion
widths here; added for clarity. -->
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="0.5*" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Background="Tomato"
TextWrapping="Wrap">I'm on the left</TextBlock>
<TextBlock
Grid.Column="1"
Background="Yellow"
TextAlignment="Right"
TextWrapping="Wrap">I'm on the right</TextBlock>
</Grid>
<Grid Grid.Row="1" Background="Gray" />
<DockPanel Grid.Row="2">
<TextBlock
DockPanel.Dock="Left"
Background="Tomato"
TextWrapping="Wrap">I'm on the left</TextBlock>
<TextBlock
DockPanel.Dock="Right"
Background="Yellow"
TextAlignment="Right"
TextWrapping="Wrap">I'm on the right</TextBlock>
</DockPanel>
</Grid>
</Page>