草庐IT

WPF——给button添加背景图片

光怪陆离的节日 2023-06-02 原文

只是想做一个很简单的图片按钮而已,不需要那么复杂。

=============================================================================================

在WPF中,如果要想给按钮控件Button加上图片,最直接的做法是修改控件模板,在模板中加入想要的图片,代码如下图所示:

复制代码

<Button.Template>





</Button.Template>

复制代码

   但是这样做有一个弊端——每次需要用到图片按钮的时候都要去修改模板。因为上面的示例代码中,模板代码过于精简,所以乍看之下似乎这种做法也没有什么不好。但是在实际的应用中,按钮控件的模板往往复杂得多,比如,有很多的Trigger事件,往往需要根据鼠标或按钮的状态来调整控件的图片、字体、背景等状态。因此,如果每次应用图片控件的时候都修改模板,很可能会导致xaml文件的代码量爆炸。
  先给出一个简单的MVVM的绑定方式,供大家参考吧,后面会给出控件重写的方案。

复制代码

下面是控件重写的方案:
一个可行的解决方案为,封装一个用于图片按钮的自定义按钮控件,该控件继承自Button控件,但是额外增加了一些用户图片绑定的依赖属性,同时在控件的默认外观模板中,通过TemplateBinding的方式绑定到依赖属性上,这样在使用的时候便可以直接通过绑定的方式设置图片按钮需要显示的图片,不再需要修改控件模板。

   其实现方式如下:



   一  代码结构
   如图所示,

采用自定义控件(CustomControl)的方式对Button控件进行封装。其中ImageButton.xaml为默认控件模板,ImageButton.cs为控件的逻辑控制文件,其中包含了ImageButton控件所需要的新的依赖属性,包括图片源属性等。

   二 模板代码

复制代码

<Style TargetType="{x:Type local:ImageButton}">  
    <Setter Property="Cursor" Value="Hand"/>  
    <Setter Property="Template">  
        <Setter.Value>  
            <ControlTemplate TargetType="{x:Type local:ImageButton}">  

                <Border Background="{TemplateBinding Background}"  
                        BorderBrush="{TemplateBinding BorderBrush}"  
                        BorderThickness="{TemplateBinding BorderThickness}">  
                    <Grid x:Name="grid" Background="{TemplateBinding Background}">  
                        <Border x:Name="PART_Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}"/>  

                        <Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">  
                            <StackPanel HorizontalAlignment="Center" Orientation="{TemplateBinding IconContentOrientation}" VerticalAlignment="Center" Margin="{TemplateBinding Padding}">  
                                <Grid HorizontalAlignment="Center" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">  
                                    <Image x:Name="PART_Icon" Source="{TemplateBinding Icon}" Height="{TemplateBinding IconHeight}" Width="{TemplateBinding IconWidth}"/>  
                                    <Image x:Name="PART_MouseOverIcon" Visibility="Collapsed" Source="{TemplateBinding IconMouseOver}"  Height="{TemplateBinding IconHeight}" Width="{TemplateBinding IconWidth}"/>  
                                    <Image x:Name="PART_PressIcon" Visibility="Collapsed" Source="{TemplateBinding IconPress}"  Height="{TemplateBinding IconHeight}" Width="{TemplateBinding IconWidth}"/>  
                                </Grid>  
                                <TextBlock x:Name="PART_Content" Text="{TemplateBinding Content}"   
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"   
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"  
                                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"    
                                           Margin="{TemplateBinding IconContentMargin}"  
                                           Foreground="{TemplateBinding Foreground}"   
                                           TextTrimming="CharacterEllipsis"/>  
                            </StackPanel>  
                        </Grid>  
                    </Grid>  
                </Border>  

                <ControlTemplate.Triggers>  

                    <Trigger Property="IsMouseOver" Value="True">  
                        <Setter Property="Foreground" TargetName="PART_Content" Value="{Binding MouseOverForeground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>  
                        <Setter Property="Background" TargetName="PART_Border" Value="{Binding MouseOverBackground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>  
                        <Setter Property="BorderBrush" TargetName="PART_Border" Value="{Binding MouseOverBorderBrush,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>  
                        <Setter Property="Visibility" TargetName="PART_MouseOverIcon" Value="Visible"/>  
                        <Setter Property="Visibility" TargetName="PART_Icon" Value="Collapsed"/>  
                        <Setter Property="Visibility" TargetName="PART_PressIcon" Value="Collapsed"/>  
                    </Trigger>  

                    <Trigger Property="IsPressed" Value="True">  
                        <Setter Property="Foreground" TargetName="PART_Content" Value="{Binding MouseDownForeground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>  
                        <Setter Property="Background" TargetName="PART_Border" Value="{Binding MouseDownBackground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>  
                        <Setter Property="BorderBrush" TargetName="PART_Border" Value="{Binding MouseDownBorderBrush,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>  
                        <Setter Property="Visibility" TargetName="PART_PressIcon" Value="Visible"/>  
                        <Setter Property="Visibility" TargetName="PART_Icon" Value="Collapsed"/>  
                        <Setter Property="Visibility" TargetName="PART_MouseOverIcon" Value="Collapsed"/>  
                    </Trigger>  

                    <Trigger Property="IsEnabled" Value="False">  
                        <Setter Property="Opacity" Value="0.5" />  
                    </Trigger>  

                    <Trigger Property="Text" SourceName="PART_Content" Value="">  
                        <Setter Property="Visibility" TargetName="PART_Content" Value="Collapsed"/>  
                    </Trigger>  

                    <Trigger Property="Text" SourceName="PART_Content" Value="{x:Null}">  
                        <Setter Property="Visibility" TargetName="PART_Content" Value="Collapsed"/>  
                    </Trigger>  
                </ControlTemplate.Triggers>  
            </ControlTemplate>  
        </Setter.Value>  
    </Setter>  
</Style>  
复制代码
   在模板中,通过TemplateBinding 的方式绑定了控件中的自定义属性,并默认显示给定的图标和文字。
   然后通过触发器,当鼠标悬停或按下的时候,控制相关图标的显示隐藏以及文字的背景色、前景色和边框颜色。


   三 自定义依赖属性
   在ImageButton.cs中定义依赖属性,这些依赖属性包含了图片按钮控件的边框、前景色、背景色,图片等属性。在复写的OnApplyTemplate方法中,会判断如果某些依赖属性的值为null,则使用默认属性。

复制代码
public class ImageButton : Button
{
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
}

public override void OnApplyTemplate()  
{  
    base.OnApplyTemplate();  
    if (this.MouseOverBackground == null)  
    {  
        this.MouseOverBackground = Background;  
    }  
    if (this.MouseDownBackground == null)  
    {  
        if (this.MouseOverBackground == null)  
        {  
            this.MouseDownBackground = Background;  
        }  
        else  
        {  
            this.MouseDownBackground = MouseOverBackground;  
        }  
    }  
    if (this.MouseOverBorderBrush == null)  
    {  
        this.MouseOverBorderBrush = BorderBrush;  
    }  
    if (this.MouseDownBorderBrush == null)  
    {  
        if (this.MouseOverBorderBrush == null)  
        {  
            this.MouseDownBorderBrush = BorderBrush;  
        }  
        else  
        {  
            this.MouseDownBorderBrush = MouseOverBorderBrush;  
        }  
    }  
    if (this.MouseOverForeground == null)  
    {  
        this.MouseOverForeground = Foreground;  
    }  
    if (this.MouseDownForeground == null)  
    {  
        if (this.MouseOverForeground == null)  
        {  
            this.MouseDownForeground = Foreground;  
        }  
        else  
        {  
            this.MouseDownForeground = this.MouseOverForeground;  
        }  
    }  
}  

#region Dependency Properties  

/// <summary>  
/// 鼠标移上去的背景颜色  
/// </summary>  
public static readonly DependencyProperty MouseOverBackgroundProperty  
    = DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(ImageButton));  

/// <summary>  
/// 鼠标按下去的背景颜色  
/// </summary>  
public static readonly DependencyProperty MouseDownBackgroundProperty  
    = DependencyProperty.Register("MouseDownBackground", typeof(Brush), typeof(ImageButton));  

/// <summary>  
/// 鼠标移上去的字体颜色  
/// </summary>  
public static readonly DependencyProperty MouseOverForegroundProperty  
    = DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  

/// <summary>  
/// 鼠标按下去的字体颜色  
/// </summary>  
public static readonly DependencyProperty MouseDownForegroundProperty  
    = DependencyProperty.Register("MouseDownForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  

/// <summary>  
/// 鼠标移上去的边框颜色  
/// </summary>  
public static readonly DependencyProperty MouseOverBorderBrushProperty  
    = DependencyProperty.Register("MouseOverBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  

/// <summary>  
/// 鼠标按下去的边框颜色  
/// </summary>  
public static readonly DependencyProperty MouseDownBorderBrushProperty  
    = DependencyProperty.Register("MouseDownBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  

/// <summary>  
/// 圆角  
/// </summary>  
public static readonly DependencyProperty CornerRadiusProperty  
    = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ImageButton), null);  

//图标  
public static readonly DependencyProperty IconProperty  
    = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(ImageButton), null);  

//鼠标移上去的图标图标  
public static readonly DependencyProperty IconMouseOverProperty  
    = DependencyProperty.Register("IconMouseOver", typeof(ImageSource), typeof(ImageButton), null);  

//鼠标按下去的图标图标  
public static readonly DependencyProperty IconPressProperty  
    = DependencyProperty.Register("IconPress", typeof(ImageSource), typeof(ImageButton), null);  

//图标高度  
public static readonly DependencyProperty IconHeightProperty  
    = DependencyProperty.Register("IconHeight", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));  

//图标宽度  
public static readonly DependencyProperty IconWidthProperty  
    = DependencyProperty.Register("IconWidth", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));  

//图标和内容的对齐方式  
public static readonly DependencyProperty IconContentOrientationProperty  
    = DependencyProperty.Register("IconContentOrientation", typeof(Orientation), typeof(ImageButton), new PropertyMetadata(Orientation.Horizontal, null));  

//图标和内容的距离  
public static readonly DependencyProperty IconContentMarginProperty  
    = DependencyProperty.Register("IconContentMargin", typeof(Thickness), typeof(ImageButton), new PropertyMetadata(new Thickness(0, 0, 0, 0), null));  

#endregion  

#region Property Wrappers  

public Brush MouseOverBackground  
{  
    get  
    {  
        return (Brush)GetValue(MouseOverBackgroundProperty);  
    }  
    set { SetValue(MouseOverBackgroundProperty, value); }  
}  

public Brush MouseDownBackground  
{  
    get  
    {  
        return (Brush)GetValue(MouseDownBackgroundProperty);  
    }  
    set { SetValue(MouseDownBackgroundProperty, value); }  
}  

public Brush MouseOverForeground  
{  
    get  
    {  
        return (Brush)GetValue(MouseOverForegroundProperty);  
    }  
    set { SetValue(MouseOverForegroundProperty, value); }  
}  

public Brush MouseDownForeground  
{  
    get  
    {  
        return (Brush)GetValue(MouseDownForegroundProperty);  
    }  
    set { SetValue(MouseDownForegroundProperty, value); }  
}  

public Brush MouseOverBorderBrush  
{  
    get { return (Brush)GetValue(MouseOverBorderBrushProperty); }  
    set { SetValue(MouseOverBorderBrushProperty, value); }  
}  

public Brush MouseDownBorderBrush  
{  
    get { return (Brush)GetValue(MouseDownBorderBrushProperty); }  
    set { SetValue(MouseDownBorderBrushProperty, value); }  
}  

public CornerRadius CornerRadius  
{  
    get { return (CornerRadius)GetValue(CornerRadiusProperty); }  
    set { SetValue(CornerRadiusProperty, value); }  
}  

public ImageSource Icon  
{  
    get { return (ImageSource)GetValue(IconProperty); }  
    set { SetValue(IconProperty, value); }  
}  

public ImageSource IconMouseOver  
{  
    get { return (ImageSource)GetValue(IconMouseOverProperty); }  
    set { SetValue(IconMouseOverProperty, value); }  
}  

public ImageSource IconPress  
{  
    get { return (ImageSource)GetValue(IconPressProperty); }  
    set { SetValue(IconPressProperty, value); }  
}  

public double IconHeight  
{  
    get { return (double)GetValue(IconHeightProperty); }  
    set { SetValue(IconHeightProperty, value); }  
}  

public double IconWidth  
{  
    get { return (double)GetValue(IconWidthProperty); }  
    set { SetValue(IconWidthProperty, value); }  
}  

public Orientation IconContentOrientation  
{  
    get { return (Orientation)GetValue(IconContentOrientationProperty); }  
    set { SetValue(IconContentOrientationProperty, value); }  
}  

public Thickness IconContentMargin  
{  
    get { return (Thickness)GetValue(IconContentMarginProperty); }  
    set { SetValue(IconContentMarginProperty, value); }  
}  

#endregion  

}
复制代码

   四 控件的应用


   应用控件的时候,只需要简单的绑定控件的相关属性即可。

有关WPF——给button添加背景图片的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  4. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  5. ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题 - 2

    我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植

  6. ruby-on-rails - Ruby on Rails - 为文本区域和图片生成列 - 2

    我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数

  7. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  8. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  9. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  10. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

随机推荐