2009-10-15 80 views
0

我有以下爲UserControl的身體:WPF:如何將內容控件包裝在另一箇中?

<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> 

但是,這並沒有給我我所期望的效果。我在這裏犯了什麼明顯的錯誤?

回答

1

你可以用一個簡單的風格做到這一點(如果我正確地得到你)。

<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> 
相關問題