XAML 如何使TextBlock右对齐?

dldeef67  于 2023-01-28  发布在  其他
关注(0)|答案(4)|浏览(330)

如何使下面状态栏中的TextBlock右对齐?
我告诉它:

  • 水平对齐=“右”
  • 文本对齐方式=“右”

但是文字还不听话地坐在左边,我还有什么要说的呢?

<Window x:Class="TestEvents124.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300"
        MaxWidth="700" Width="700"
        >
    <DockPanel HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto">

        <StatusBar Width="Auto" Height="25" Background="#888" DockPanel.Dock="Bottom" HorizontalAlignment="Stretch">
            <TextBlock 
                Width="Auto" 
                Height="Auto" 
                Foreground="#fff" 
                Text="This is the footer." 
                HorizontalAlignment="Right"
                TextAlignment="Right"
                />
        </StatusBar>

        <GroupBox DockPanel.Dock="Top" Height="Auto" Header="Main Content">
            <WrapPanel Width="Auto" Height="Auto">
                <TextBlock Width="Auto" Height="Auto" TextWrapping="Wrap" Padding="10">
                This is an example of the content, it will be swapped out here.
                </TextBlock>
            </WrapPanel>
        </GroupBox>

    </DockPanel>

</Window>
mqxuamgl

mqxuamgl1#

我对您的代码进行了一次尝试,通过使用StatusBarItem而不是TextBlock,设法使它看起来“正确”(没有双关语):

<StatusBar Width="Auto" Height="25" 
    Background="#888" DockPanel.Dock="Bottom" 
    HorizontalAlignment="Stretch" >
    <StatusBarItem Foreground="#fff" 
        HorizontalContentAlignment="Right">This is the footer</StatusBarItem>
</StatusBar>

不确定TextBlock发生了什么-我的经验告诉我HorizontalContentAlignment和HorizontalAlignment的某种组合(在StatusBar和TextBlock上)应该可以达到你想要的效果。无论如何-希望StatusBarItem能为你工作。

cclgggtu

cclgggtu2#

<StatusBar>
    <StatusBar.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="100"/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </StatusBar.ItemsPanel>
    <StatusBarItem Grid.Column="0">
        <TextBlock>something</TextBlock>
    </StatusBarItem>
    <Separator Grid.Column="1" />
    <StatusBarItem Grid.Column="2">
        <TextBlock>logged in</TextBlock>
    </StatusBarItem>
</StatusBar>

这个例子不会弄乱你的分隔符。基于http://kent-boogaart.com/blog/the-perfect-wpf-statusbar的例子
您不应该将分隔符放在StatusBarItem中,它会将分隔符缩小为一个点。

nvbavucw

nvbavucw3#

对于任何在标题中寻找问题答案的人(不一定要在状态栏中使用),我发现Label比TextBlock更好,因为它可以控制对齐,而且仍然感觉语义正确。

w1e3prcc

w1e3prcc4#

看起来这仍然是一个问题。问题是TextBlock的宽度是根据其内容自动设置的。要验证这一点,只需将Background属性设置为另一种颜色。将HorizontalAlignment设置为Stretch没有帮助。幸运的是,我的大多数属性都是用代码设置的(MPVM)和我的文本块包含在一个面板中,我能够将文本块的宽度属性设置为其父宽度。即tb.Width = tb.Parent.Width,然后右对齐起作用。

相关问题