ContentPresenter-水平对齐不起作用

时间:2018-08-27 05:38:07

标签: c# wpf xaml controls alignment

我已经创建了用于简化label: control格式的自定义控件:

enter image description here

我的问题是,无论将什么放入控件内容中,我都无法使用模板代码对齐它。唯一有效的方法是将HorizontalAlignment="Right"直接放入TextBox控件中:

<local:ControlEntry Label="Enter something">

    <!--  I don't like having HorizontalAlignment here!  -->
    <TextBox Height="20" Width="200" HorizontalAlignment="Right" />

</local:ControlEntry>

但是,我想在控制模板的后台进行此操作。

我正在谈论这个特定的XAML代码:

<Border x:Name="Container"
        HorizontalAlignment="Stretch"
        Grid.Column="1">

    <ContentPresenter Width="{Binding ActualWidth, ElementName=Container}" HorizontalAlignment="Right" />

</Border>

首先,我拉伸HorizontalAlignment,使其填充网格列单元格的宽度,然后尝试将Right HorizontalAlignment应用于{{1 }}。不幸的是,看不到任何效果。

完整控制代码:

ContentPresenter

如何正确地保持宽度伸展,同时将<Style TargetType="{x:Type local:ControlLine}"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ControlLine}"> <!-- Create bottom content border --> <Border BorderBrush="#000" BorderThickness="0,0,0,0.5"> <StackPanel> <Grid> <!-- Label -> Control --> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <!-- Label --> <TextBlock Grid.Column="0" Margin="0,12,15,12" VerticalAlignment="Center" Text="{Binding Label, RelativeSource={RelativeSource TemplatedParent}}" TextWrapping="WrapWithOverflow" /> <!-- Display content --> <Border x:Name="Container" HorizontalAlignment="Stretch" Grid.Column="1"> <ContentPresenter Width="{Binding ActualWidth, ElementName=Container}" HorizontalAlignment="Right" /> </Border> </Grid> <!-- Description --> <TextBlock Text="The control description" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> 的内容向右对齐?

0 个答案:

没有答案