Wpf 动态颜色填充矩形

Wpf 动态颜色填充矩形,wpf,label,fill,Wpf,Label,Fill,我尝试为我的标签创建一个ControlTemplate,如下所示: <Style TargetType="{x:Type Label}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Label}"> <Grid x:Name="LayoutRoo

我尝试为我的标签创建一个ControlTemplate,如下所示:

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

我想在创建控件时填充矩形的颜色,例如:

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

那个么,在创建控件时,如何更改controlTemplate中的属性“Fill”来动态设置矩形的颜色呢

非常感谢

编辑:这是解决方案,我创建了一个新类,继承自标签,如下所示:

<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>
    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
公共类lbltitrachamp
继承标签
作为从属属性的公共共享只读库属性=
Register(“CouleurRectangle”、GetType(SolidColorBrush)、GetType(lbltitrachamp))
''但标签上写的东西是正确的
公共财产CouleurRectangle作为SolidColorBrush
收到
返回GetValue(CouleurProperty)
结束
设置(ByVal值作为SolidColorBrush)
设置值(CouleurProperty,value)
端集
端属性
末级
然后,在我的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>

最后,创建一个新标签:

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


非常感谢:)

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

由于您的标签不是
CustomControl
,因此您无法动态地在其上创建属性,因此最好的方法是使用Label的background属性

...
<Rectangle Height="30" ...
                       Fill="{TemplateBinding Background}" />
...
。。。
...
否则,创建一个继承
标签的
CustomControl
,创建一个新的依赖属性,并为其定义一个XAML模板样式