代码之家  ›  专栏  ›  技术社区  ›  Pieter Breed

WPF:如何将内容控件包装在另一个控件中?

  •  0
  • Pieter Breed  · 技术社区  · 15 年前

    我将以下内容作为 UserControl :

    <Label FontWeight="Bold"
           x:Name="PaletteLabel"
           HorizontalAlignment="Stretch"
           BorderThickness="1"
           >
        <Label.Background>
            <LinearGradientBrush EndPoint="0.5,1"
                                 StartPoint="0.5,0">
                <GradientStop Color="#FFB6B5C3"
                              Offset="0" />
                <GradientStop Color="#FFF4F4F6"
                              Offset="1" />
            </LinearGradientBrush>
        </Label.Background>
        <ContentPresenter  />
    </Label>
    

    我希望能够像这样使用它:

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

    但这并没有给我预期的效果。我在这里犯了什么明显的错误吗?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Rich    15 年前

    你可以用一种简单的方式来完成这个(如果我能正确地理解你的话)。

    <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>
    
        2
  •  0
  •   Community CDub    8 年前