2012-07-03 184 views
1

我尝试创建一个的ControlTemplate我的标签是这样的:WPF,动态颜色填充矩形

<Style TargetType="{x:Type Label}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Label}"> 
       <Grid x:Name="LayoutRoot" Background="White"> 
        <Rectangle Height="30" HorizontalAlignment="Left" Margin="10" 
           Stroke="transparent" 
           VerticalAlignment="Top" Width="3" 
           Fill="#FF259FED" /> 

        <ContentPresenter HorizontalAlignment="Left" Margin="17,0,0,0" RecognizesAccessKey="True" 
             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
             VerticalAlignment="Center"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Foreground" Value="#7A7E81" /> 
    <Setter Property="FontWeight" Value="Bold" /> 
</Style> 

我要填写矩形的颜色,当我创造了我的控制,像这样的例子:

<Label Content="Prénom" VerticalAlignment="Top" CouleurRectangle="#FF259FED" /> 

那么,如何在我的controlTemplate中更改Property“Fill”以在创建控件时动态地设置矩形的颜色?

非常感谢。

编辑:这是解决方案,我创建谁从标签继承这样一个新的类:

Public Class LblTitreChamp 
    Inherits Label 

    Public Shared ReadOnly CouleurProperty As DependencyProperty = 
     DependencyProperty.Register("CouleurRectangle", GetType(SolidColorBrush), GetType(LblTitreChamp)) 
    ''' <summary>Propriété pour insérer une couleur au début du Label</summary> 
    Public Property CouleurRectangle As SolidColorBrush 
     Get 
      Return GetValue(CouleurProperty) 
     End Get 
     Set(ByVal value As SolidColorBrush) 
      SetValue(CouleurProperty, value) 
     End Set 
    End Property 

End Class 

然后,在我的控件模板:

<Style TargetType="{x:Type local:LblTitreChamp}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:LblTitreChamp}"> 
        <Grid x:Name="LayoutRoot" Background="White"> 
         <Rectangle Height="30" HorizontalAlignment="Left" Margin="10" 
            Stroke="transparent" 
            VerticalAlignment="Top" Width="3" 
            Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Label}}, Path=CouleurRectangle}"/> 

         <ContentPresenter HorizontalAlignment="Left" Margin="17,0,0,0" RecognizesAccessKey="True" 
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
              VerticalAlignment="Center"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Foreground" Value="#7A7E81" /> 
     <Setter Property="FontWeight" Value="Bold" /> 
    </Style> 

最后,以创造一个新的标签:

<my:LblTitreChamp Content="ID" VerticalAlignment="Top" CouleurRectangle="Black" /> 

感谢的很多关于你:)

回答

0

hi set Fill = {TemplateBinding CouleurRectangle}。希望这会有所帮助。而且我期望你已经创建了一个自定义Label类,该类继承自Label并具有DependencyProperty CouleurRectangle。

0

由于您的标签不是CustomControl,那么你就不能动态地使属性上,因此最好的办法是使用标签

... 
<Rectangle Height="30" ... 
         Fill="{TemplateBinding Background}" /> 
... 

否则的背景属性创建一个CustomControl继承Label,使一个新的依赖属性,并为其定义一个XAML模板样式。