微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

在我的例子中,我有一个显示“hello world”的内容控件和一个显示“change it”的按钮.

当我单击按钮时,我在内容控件和按钮上设置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

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐