Hi, I am trying to get a rounded-rectangle Label, this method has worked fine for my TextBox, and seems to work for my Label except the Content doesn't appear (ie, there is no text). When I remove the <Setter Property="Template"> section, the label goes square but the text is visible. Can anyone spot what I'm doing wrong?

Here is my label style:

<Style x:Key="StyleLabel" TargetType="Label">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="6,6,6,6">
                    <ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="PART_ContentHost" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Padding" Value="8,4" />
    <Setter Property="Margin" Value="4" />
    <Setter Property="Height" Value="26" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Width" Value="Auto" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="BorderBrush" Value="#646464" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Background" Value="#323232" />
    <Setter Property="Foreground" Value="yellow" />
</Style>

here is my working text box version:

<Style x:Key="ActiveTextBox" TargetType="TextBox">
    <Setter Property="Background" Value="#005500" />
    <Setter Property="Foreground" Value="yellow" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="6,6,6,6">
                    <ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="PART_ContentHost" HorizontalAlignment="Right" VerticalAlignment="Center" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="White" />
                        <Setter Property="BorderThickness" Value="3"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Padding" Value="8,4" />
    <Setter Property="Margin" Value="4" />
    <Setter Property="Height" Value="26" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Width" Value="Auto" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />

    <Setter Property="TextAlignment" Value="Center" />
    <Setter Property="BorderBrush" Value="#646464" />
</Style>

Solved it!

For the record, I needed:

<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

instead of the ScrollViewer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.