开发者

WPF: How can I wrap a content control in another?

I have the following as the body of a UserControl:

<Label FontWeight="Bold"
       x:Name="PaletteLabel"
       HorizontalAlignment="Stretch"
       BorderThickness="1"
       >
    <Label.Background>
        <LinearGradientBrush EndPoint="0.5,1"
                             StartPo开发者_高级运维int="0.5,0">
            <GradientStop Color="#FFB6B5C3"
                          Offset="0" />
            <GradientStop Color="#FFF4F4F6"
                          Offset="1" />
        </LinearGradientBrush>
    </Label.Background>
    <ContentPresenter  />
</Label>

I expect to be able to use it like this:

<uc:NiceLabel>Text Content</uc:NiceLabel>

But that does not give me the effect I expect. Am I making any obvious mistakes here?


You can accomplish this with a simple style (if I'm getting you correctly).

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type Label}" x:Key="NiceLabelStyle">
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1"
                             StartPoint="0.5,0">
                        <GradientStop Color="#FFB6B5C3"
                          Offset="0" />
                        <GradientStop Color="#FFF4F4F6"
                          Offset="1" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Label Style="{StaticResource NiceLabelStyle}">Test</Label>
    </StackPanel>
</Window>


Possibly covered in How to create a WPF UserControl with NAMED content

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜