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

发布于 2025-01-07 08:45:25 字数 978 浏览 3 评论 0原文

请帮助我编辑 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>

所以它应该像

在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2025-01-14 08:45:25

我会使用 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>

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>
时光瘦了 2025-01-14 08:45:25

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

<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 列所需的空间除外。

If I were doing this I would use a grid rather than a horizontal stack panel:

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

Note the * in the column 3 which means that column will use all available space after column 2, except for what is needed by column 4.

终陌 2025-01-14 08:45:25

不要使用 StackPanel 而是使用 网格 包含列?

Don't use a StackPanel but a Grid with columns?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文