Search code examples
wpfxamltextblock

How to bind WPF TextBlock to right top corner?


Please help me to edit XAML so the TextBlock which shows minutes goes to the right top corner.

<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>

So it should be like

enter image description here


Solution

  • I would use a DockPanel. For the child nodes, just add DockPanel.Dock attributes to indicate where you want the element to go. The last child element will automatically fill the remaining area.

    <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>