zoukankan      html  css  js  c++  java
  • 2018-8-10-WPF-修改按钮按下的颜色

    title author date CreateTime categories
    WPF 修改按钮按下的颜色
    lindexi
    2018-08-10 19:16:53 +0800
    2018-03-15 20:26:22 +0800
    WPF

    本文告诉大家如何使用附加属性修改按钮按下去时的背景

    先让大家看个图片,下面来告诉大家如何做

    首先在后台创建一个附加属性

        public class ButtonBrush
        {
            public static readonly DependencyProperty ButtonPressBackgroundProperty = DependencyProperty.RegisterAttached(
                "ButtonPressBackground", typeof(Brush), typeof(ButtonBrush), new PropertyMetadata(default(Brush)));
    
            public static void SetButtonPressBackground(DependencyObject element, Brush value)
            {
                element.SetValue(ButtonPressBackgroundProperty, value);
            }
    
            public static Brush GetButtonPressBackground(DependencyObject element)
            {
                return (Brush) element.GetValue(ButtonPressBackgroundProperty);
            }
        }
    

    然后在 xaml 使用附加属性

            <Button Margin="10,10,10,10" 
                    Width="300" Height="100"
                    Content="确定"
                    local:ButtonBrush.ButtonPressBackground="#FFfcac1c" />

    如何在按钮按下时使用这个附加属性修改按钮颜色?实际重写按钮的样式可以看到,在按下时可以修改颜色

            <Style x:Key="Style.OkOperationButton"
                   TargetType="ButtonBase">
                <Setter Property="Width" Value="110" />
                <Setter Property="Height" Value="44" />
                <Setter Property="FontSize" Value="24" />
                <Setter Property="Background" Value="#FF0087FF" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ButtonBase}">
                            <Border x:Name="Border" Width="{TemplateBinding Width}"
                                    Height="{TemplateBinding Height}"
                                    CornerRadius="22" Background="{TemplateBinding Background}">
                                <TextBlock x:Name="TextBlock"
                                           Text="{TemplateBinding Content}"
                                           FontSize="{TemplateBinding FontSize}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Border>
    
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Background"
                                            Value="#FFfcac1c" />
                                    <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter TargetName="Border" Property="Background" Value="#4D0087FF" />
                                    <Setter TargetName="TextBlock" Property="Foreground" Value="#4DFFFFFF" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    那么如何在设置使用附加属性,实际上使用下面的代码直接从按钮获取附加属性

                      <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Background"
                                            Value="{Binding RelativeSource = {RelativeSource Self},Path=(local:ButtonBrush.ButtonPressBackground)}" />
                                    <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
                                </Trigger>

    所有的代码

     <Window.Resources>
    
            <Style x:Key="Style.OkOperationButton"
                   TargetType="ButtonBase">
                <Setter Property="Width" Value="110" />
                <Setter Property="Height" Value="44" />
                <Setter Property="FontSize" Value="24" />
                <Setter Property="Background" Value="#FF0087FF" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ButtonBase}">
                            <Border x:Name="Border" Width="{TemplateBinding Width}"
                                    Height="{TemplateBinding Height}"
                                    CornerRadius="22" Background="{TemplateBinding Background}">
                                <TextBlock x:Name="TextBlock"
                                           Text="{TemplateBinding Content}"
                                           FontSize="{TemplateBinding FontSize}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Border>
    
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Background"
                                            Value="{Binding RelativeSource = {RelativeSource Self},Path=(local:ButtonBrush.ButtonPressBackground)}" />
                                    <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter TargetName="Border" Property="Background" Value="#4D0087FF" />
                                    <Setter TargetName="TextBlock" Property="Foreground" Value="#4DFFFFFF" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
        </Window.Resources>
        <Grid>
            <Button Margin="10,10,10,10" Style="{StaticResource Style.OkOperationButton}"
                    Width="300" Height="100"
                    Content="确定"
                    local:ButtonBrush.ButtonPressBackground="#FFfcac1c" />
        </Grid>

    代码:下载

  • 相关阅读:
    遗传算法的几种改进
    python3中让程序暂停运行的语句
    python内存增长问题
    关于编程思路
    关于程序中delay函数带来的繁琐问题
    python一切皆对象的理解
    Inspector did not run successfully.
    ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 401 Unauthor.
    决策树的升入浅出-视频
    -bash: /opt/cslc/jdk1.8.0_144/bin/jps: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086132.html
Copyright © 2011-2022 走看看