简体   繁体   English

WPF:以编程方式更改具有自定义样式的控件的颜色

[英]WPF: programmatically change color of a control with a custom style

<DrawingImage x:Key="HexagonImage">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="White"  
                       Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black" Thickness="10" LineJoin="Round"/>
                </GeometryDrawing.Pen>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

<Style x:Key="HexagonButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Image x:Name="hexImg" Source="{StaticResource HexagonImage}"/>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I have a button, that has this HexagonButton as its style, and I want to change its color programmatically, Iv'e tried changing the Backgroup property, but to no avail.我有一个按钮,它的样式是 HexagonButton,我想以编程方式更改它的颜色,我尝试更改 Backgroup 属性,但无济于事。

the only way I managed to do so, is to create a whole new style, with a new Drawing image.我设法做到这一点的唯一方法是使用新的绘图图像创建一个全新的样式。 and assign it.并分配它。 But I'm certain there is an easier way to do so.但我确信有一种更简单的方法可以做到这一点。

I got it to work like by including the GeomteryDrawing directly in the Button template, and using RelativeSource bindings to the Foreground and Background properties of its Button ancestor (to which I assigned defaults in the style declaration):通过将GeomteryDrawing直接包含在Button模板中,并使用RelativeSource绑定到其Button祖先的ForegroundBackground属性(我在样式声明中为其分配了默认值),我让它像这样工作:

<Style x:Key="HexagonButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="White" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Image x:Name="hexImg">
                        <Image.Source>
                            <DrawingImage>
                                <DrawingImage.Drawing>
                                    <DrawingGroup>
                                        <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=Background}" Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z">
                                            <GeometryDrawing.Pen>
                                                <Pen Brush="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=Foreground}" Thickness="10" LineJoin="Round" />
                                            </GeometryDrawing.Pen>
                                        </GeometryDrawing>
                                    </DrawingGroup>
                                </DrawingImage.Drawing>
                            </DrawingImage>
                        </Image.Source>
                    </Image>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The default white and black button is then:然后默认的白色和黑色按钮是:

<Button Style="{StaticResource HexagonButton}">Click me</Button>

And a custom button is:自定义按钮是:

<Button Style="{StaticResource HexagonButton}" Background="Yellow" Foreground="Red">Click me</Button>

If you want to change the background only around the hexagon, add Background="{TemplateBinding Background}" to the Grid in your ControlTemplate .如果您只想更改六边形周围的背景,请将Background="{TemplateBinding Background}"添加到ControlTemplate中的Grid中。

If you also want to change the background color of the inside of the hexagon, change the Brush of your GeometryDrawing to Transparent .如果您还想更改六边形内部的背景颜色,请将GeometryDrawingBrush更改为Transparent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM