如何将WPF TextBlock绑定到右上角?

时间:2012-02-20 17:03:06

标签: wpf xaml textblock

请帮助我修改XAML,以便显示分钟TextBlock转到右上角。

<StackPanel Orientation="Horizontal" >
   <StackPanel Orientation="Horizontal" >
      <TextBlock Name="UserNameTextBlock"  Margin="0,0,8,0"  VerticalAlignment="Bottom" FontSize="15" Text="{Binding Path=UserName}" FontWeight="Bold"></TextBlock>
      <TextBlock   FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Foreground="LightGray" >@</TextBlock>
      <TextBlock  FontSize="13"  VerticalAlignment="Bottom" Padding="0,0,0,1" Name="ScreenNameTextBlock"  Text="{Binding Path=ScreenName}" Foreground="Gray" ></TextBlock>
      <TextBlock  FontSize="13"  VerticalAlignment="Bottom" Padding="0,0,0,1" Name="MinAgo"  Text="{Binding Path=MinAgo}" Foreground="Gray" ></TextBlock>    
     </StackPanel>
</StackPanel>

所以它应该像

enter image description here

3 个答案:

答案 0 :(得分:5)

我会使用DockPanel。对于子节点,只需添加DockPanel.Dock属性即可指示元素的去向。最后一个子元素将自动填充剩余区域。

<DockPanel>
    <TextBlock DockPanel.Dock="Right" FontSize="13"  VerticalAlignment="Bottom" Padding="0,0,0,1" Name="MinAgo"  Text="{Binding Path=MinAgo}" Foreground="Gray" ></TextBlock>    
    <StackPanel Orientation="Horizontal" >
        <TextBlock Name="UserNameTextBlock"  Margin="0,0,8,0"  VerticalAlignment="Bottom" FontSize="15" Text="{Binding Path=UserName}" FontWeight="Bold"></TextBlock>
        <TextBlock FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Foreground="LightGray" >@</TextBlock>
        <TextBlock FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Name="ScreenNameTextBlock"  Text="{Binding Path=ScreenName}" Foreground="Gray" ></TextBlock>
    </StackPanel>
</DockPanel>

答案 1 :(得分:4)

如果我这样做,我会使用网格而不是水平堆栈面板:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Name="UserNameTextBlock"  Margin="0,0,8,0"  VerticalAlignment="Bottom" FontSize="15" Text="{Binding Path=UserName}" FontWeight="Bold"></TextBlock>
    <TextBlock Grid.Column="1" FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Foreground="LightGray" >@</TextBlock>
    <TextBlock Grid.Column="2"  FontSize="13"  VerticalAlignment="Bottom" Padding="0,0,0,1" Name="ScreenNameTextBlock"  Text="{Binding Path=ScreenName}" Foreground="Gray" ></TextBlock>
    <TextBlock Grid.Column="4"  FontSize="13"  VerticalAlignment="Bottom" Padding="0,0,0,1" Name="MinAgo"  Text="{Binding Path=MinAgo}" Foreground="Gray" ></TextBlock>
</Grid>

注意第3列中的*表示该列将使用第2列之后的所有可用空间,但第4列除外。

答案 2 :(得分:3)

不要使用StackPanel而是使用Grid列吗?