使用Horizo​​ntalContentAlignment属性将Text与ContentControl对齐

时间:2013-02-19 20:30:56

标签: wpf alignment text-alignment text-align

我正在尝试将“文本对齐”应用于ContentControl。由于ContentControl没有像TextBlock那样的水平或垂直文本对齐属性,因此我尝试使用ContentControl的Horizo​​ntalContentAlignment属性。

我的问题是我无法使用ContentControl本身。

在我的例子中,我有一个显示“hello world”的内容控件和一个显示“更改它”的按钮。

当我单击按钮时,我在内容控件和按钮上设置Horizo​​ntalContentAlignment。按钮的内容会更改,但内容控件的内容不会更改。

这是我的XAML代码:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ContentControl x:Name="ctrl" Width="525">
        Hello World!
    </ContentControl>
    <Button x:Name="btn" Grid.Row="1" Content="Change It" Click="btn_Click"/>
</Grid>
</Window>

这是按钮点击事件的VB.NET代码:

Class MainWindow 

  Private Sub btn_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    If (ctrl.HorizontalContentAlignment = HorizontalAlignment.Left) Then
        ctrl.HorizontalContentAlignment = HorizontalAlignment.Right
        btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Right
    Else
        ctrl.HorizontalContentAlignment = HorizontalAlignment.Left
        btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Left
    End If
    ctrl.UpdateLayout()
  End Sub

End Class

由于各种原因,我无法用文本块替换我的内容控件,但我仍然需要能够对齐内容。

编辑:

虽然Narohi解决了建议的工作原理,但我仍然对内容控件的Horizo​​ntalContentAlignment属性不对齐内容感到困惑。

我尝试了一个Label控件(继承自ContentControl),并且它的Horizo​​ntalContentAlignment属性正确对齐内容。

(再次编辑:我不再对此感到困惑,似乎在所有情况下都没有正确使用Horizo​​ntalContentAlignment。)

这是我更新的XAML代码:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ControlTemplate x:Key="AlignmentAwareControl" TargetType="ContentControl">
            <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ContentControl x:Name="ctrlTxt" Grid.Row="0"
                    Template="{StaticResource AlignmentAwareControl}" 
                    HorizontalContentAlignment="Center" Padding="0">Hello World Content Control!</ContentControl>
    <Label x:Name="ctrl" Grid.Row="1"  HorizontalContentAlignment="Center" Padding="0">Hello World Label!</Label>
    <ContentControl x:Name="ctrlImg" Grid.Row="2" 
                    Template="{StaticResource AlignmentAwareControl}"
                    HorizontalContentAlignment="Center">
        <Image Source="C:\Users\Frinavale\Pictures\Business_Woman.jpg"/>
    </ContentControl>

    <Button x:Name="btn" Grid.Row="3" Content="Change It" Click="btn_Click"/>
</Grid>
</Window>

这是我更新的VB.NET代码:

Class MainWindow 

  Private Sub btn_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    If (ctrl.HorizontalContentAlignment = HorizontalAlignment.Left) Then
        ctrlImg.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right)
        ctrlTxt.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right)
        ctrl.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right)
        btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Right
    Else
        ctrlImg.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left)
        ctrlTxt.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left)
        ctrl.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left)
        btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Left
    End If
    ctrl.UpdateLayout()
  End Sub
End Class

我期待着你的建议,

-Frinny

1 个答案:

答案 0 :(得分:7)

在Blend中打开ContentControl的默认控件模板,可以了解原始方法无效的原因。

<ControlTemplate TargetType="{x:Type ContentControl}">
    <ContentPresenter/>
</ControlTemplate>

默认模板对从Control继承的Horizo​​ntalContentAlignment属性不执行任何操作。将此与Label的默认模板并列。

<ControlTemplate TargetType="{x:Type Label}">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

正如我们所看到的,Label实际上绑定到Horizo​​ntalContentAlignment。 WPF控件是无形的,因此永远不能保证当前ControlTemplate会尊重属性。我推测WPF设计者不尊重Horizo​​ntalContentAlignment属性,因为人们通常将内容放在其中独立于ContentControl的属性,或者他们假设有人打算使用这样的通用控件,他们会提供自己的模板,例如...

<ContentControl x:Name="ctrl" Width="525">
    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl">
            <Grid>
                <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
            </Grid>
        </ControlTemplate>
    </ContentControl.Template>
    Hello World!
</ContentControl>