Search code examples
wpflabelfill

WPF, dynamic Color Fill Rectangle


I try to create a ControlTemplate for my Label like this :

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

I want to fill color of rectangle when i create my control, like this for example :

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

So, how can I change Property "Fill" in my controlTemplate to set dynamicly color of rectangle when i create control ?

thank's a lot.

Edit : this is the solution , i create a new class who inherit from Label like this :

    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

then, in my COntrolTemplate :

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

and finally, to creat a new label :

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

thank's a lot for you :)


Solution

  • hi set Fill={TemplateBinding CouleurRectangle} .Hope this will help. And i Expect that you have created a custom Label class That inherit from Label and has DependencyProperty CouleurRectangle.