简体   繁体   中英

Overwritten properties for alignment in WPF usercontrol are not working as expected

I'm programming at this Point of time some WPF UserControls (no CustomControls!) - consisting of a handful of nested controls - and overwritting in the CS-file of the UserControl some of the inherited properties like HorizontalContentAlignment with "new". Generally my usercontrols consist of a Border and in it per example a Label with a TextBlock as Content (for the possibility of textwrapping). All properties are working well except the alignments. As Long as i used my own properties pe "ControlHorizontalContentAlignment" it worked as expected: The Label filled the Border and the TextBlock was pe at bottom right. But now with HorizontalContentAlignment overwritten, the Label don't fill the Border control anymore.

It seems to be that there is a Problem with overwritting properties like HorizontalContentAlignment. How can i handle that problem?

My UserControl for the Label is in XAML defined like that:

<UserControl x:Class="UsercontrolExample.UserControls.ControlLabel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UsercontrolExample.UserControls"
             mc:Ignorable="d"
             d:DesignHeight="30"
             d:DesignWidth="150">
    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border x:Name="border"
                Style="{StaticResource usercontrolBorderStyle}">
            <Label x:Name="label" Background="Yellow" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                   Margin="2">
                <TextBlock x:Name="textblock" Background="Orange"
                           Text="Label Label"
                           TextWrapping="Wrap"/>
            </Label>
        </Border>
    </Grid>
</UserControl>

And the Resource usercontrolBorderStyle:

<Style x:Key="usercontrolBorderStyle">
    <Setter Property="Border.BorderBrush" Value="RoyalBlue"/>
    <Setter Property="Border.BorderThickness" Value="1"/>
    <Setter Property="Border.HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Border.VerticalAlignment" Value="Stretch"/>
</Style>

This is the overwritten property:

public new HorizontalAlignment HorizontalContentAlignment
{
    get { return Converter.ConvertTextAlignmentToHorizontalAlignment(textblock.TextAlignment); }
    set {textblock.TextAlignment = Converter.ConvertHorizontalAlignmentToTextAlignment(value); }
}

And this was my own original property which worked:

public HorizontalAlignment ControlHorizontalContentAlignment
{
    get { return label.HorizontalContentAlignment; }
    set
    {
        label.HorizontalContentAlignment = value;
        textblock.TextAlignment = Converter.ConvertHorizontalAlignmentToTextAlignment(value);
    }
}

Thanks in advance!

hiding HorizontalContentAlignment property ( new HorizontalAlignment HorizontalContentAlignment ) is not a good idea. i think you can avoid that, if use a correct binding (new converter required)

<UserControl x:Class="UsercontrolExample.UserControls.ControlLabel"
             x:Name="myLabel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="LayoutRoot">
        ...
        <TextBlock x:Name="textblock" Background="Orange"
                   Text="Label Label"
                   HorizontalAlignment="Stretch"
                   TextAlignment="{Binding HorizontalContentAlignment, 
                                   ElementName=myLabel, 
                                   Converter={StaticResource HorizontalToText}}"
                   TextWrapping="Wrap"/>
    </Grid>
</UserControl>

HorizontalToText is an instance of converter stored in Resources

example of converter here: WPF binding HorizontalAlignment to TextAlignment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM