本文介绍了在WPF样式中,如何更改定义的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作图像/文本"按钮.当发生鼠标悬停事件时,我想更改绘图笔的颜色.我怎么能这样在下面的示例中,我想在鼠标悬停时使用iconBrush的颜色,红色->蓝色.

I am making Image/Text button.When mouse over event arised, I want to change color of my drawing brush.How can I this?In below example, I want to iconBrush's color, Red -> Blue when mouse over.

<SolidColorBrush x:Key="iconBrush" Color="Red"/>
<DrawingBrush x:Key="buttonIcon" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="{StaticResource iconBrush}" Geometry="... />
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

<Style x:Key="ImageTextButtonStyle" TargetType="{x:Type Button}">
    ...
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border>                        
                        <Grid>
                           <Grid.RowDefinitions>
                               <RowDefinition Height="20"/>
                               <RowDefinition Height="10"/>
                           </Grid.RowDefinitions>

                            <Grid Grid.Row="0" Background="{StaticResource buttonIcon}" />
                            <Grid Grid.Row="1">
                                <ContentPresenter ...
                                      />
                        </Grid>

                        </Grid>

                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="{StaticResource iconBrush}" Value="Blue" />
                         ---> I Want to do this way..

推荐答案

设置程序不支持嵌套属性或设置资源值,因此无法直接实现您尝试的方法

it is not possible directly the way you are trying to achieve as nested properties or setting the resources values are not supported in setters

这是一种解决方法

  • 我在网格的背景属性中移动了画笔
  • 将几何图形画笔的画笔绑定到模板化父级(即按钮)的背景
  • 然后在触发器中切换模板的背景

这是代码

<SolidColorBrush x:Key="iconBrush" Color="Red" />
<SolidColorBrush x:Key="blueBrush" Color="Blue" />
<Style x:Key="ImageTextButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{StaticResource iconBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20" />
                            <RowDefinition Height="10" />
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0">
                            <Grid.Background>
                                <DrawingBrush Stretch="Uniform">
                                    <DrawingBrush.Drawing>
                                        <DrawingGroup>
                                            <DrawingGroup.Children>
                                                <GeometryDrawing Brush="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}" Geometry="..." />
                                            </DrawingGroup.Children>
                                        </DrawingGroup>
                                    </DrawingBrush.Drawing>
                                </DrawingBrush>
                            </Grid.Background>
                        </Grid>
                        <Grid Grid.Row="1">
                            <ContentPresenter />
                        </Grid>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource blueBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

另一种方法

  • 我定义了2个图标
  • 在网格背景上设置一个图标
  • 根据触发器切换到另一个图标

代码

<DrawingBrush x.Key="redIcon" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="Red" Geometry="..." />
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

<DrawingBrush x.Key="blueIcon" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="Blue" Geometry="..." />
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

<Style x:Key="ImageTextButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20" />
                            <RowDefinition Height="10" />
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" x:Name="icon" Background="{StaticResource redIcon}" />
                        <Grid Grid.Row="1">
                            <ContentPresenter />
                        </Grid>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource blueIcon}" TargetName="icon" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

图标的附加属性

类以声明属性并保存值

class Extras: DependencyObject
{
    public static Brush GetIcon(DependencyObject obj)
    {
        return (Brush)obj.GetValue(IconProperty);
    }

    public static void SetIcon(DependencyObject obj, Brush value)
    {
        obj.SetValue(IconProperty, value);
    }

    // Using a DependencyProperty as the backing store for Icon.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IconProperty =
        DependencyProperty.RegisterAttached("Icon", typeof(Brush), typeof(Extras), new PropertyMetadata(null));
}

XAML资源

<DrawingBrush x:Key="buttonIcon"
                Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="Red"
                                    Geometry="..." />
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>
<Style x:Key="ImageTextButtonStyle"
        TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20" />
                            <RowDefinition Height="10" />
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0"
                                Background="{TemplateBinding l:Extras.Icon}" />
                        <Grid Grid.Row="1">
                            <ContentPresenter />
                        </Grid>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

用法

<Button Style="{StaticResource ImageTextButtonStyle}"
        Content="Icon Button"
        l:Extras.Icon="{StaticResource buttonIcon}"/>

这篇关于在WPF样式中,如何更改定义的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:10