图像按钮模板? [英] image button template?

查看:29
本文介绍了图像按钮模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要两种状态(正常,鼠标悬停)的图像按钮.该按钮必须使用鼠标悬停事件触发自动更改图像.此图像按钮必须是用户控件.此外,我想为我使用该用户控件的每个状态表单代码设置图像.

I want Image button with two state(normal , mouse over). that button must change image with Mouse Over event trigger automatically. this image button must be a user control. Also i want to set image for each state form code in which form i use that user control.

解决方案是使用带有值转换器"的模板,但我不知道如何?

Solution is using a template with "Value Converter" but i don't know how?

推荐答案

我在 Code-project-Article(Cool example) 上找到了这个

http://www.codeproject.com/KB/WPF/WPF_xaml_taskbar_window.aspx

首先他创建Wpf-Custom-control(你可以像这样创建从Button继承的类)

First He create Wpf-Custom-control(you can create class inherit from Button like this)

 public class ImageButton : Button
{
    private string cornerRadius;
    public string CornerRadius
    {
        get { return cornerRadius; }
        set { cornerRadius = value; }
    }

    private string highlightBackground;
    public string HighlightBackground
    {
        get { return highlightBackground; }
        set { highlightBackground = value; }
    }

    private string pressedBackground;
    public string PressedBackground
    {
        get { return pressedBackground; }
        set { pressedBackground = value; }
    }
}

作为第二步,您必须在资源字典中创建模板(这里是代码)

As second step you must Create template in resource-dictionary(here is code)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Phone.Controls">

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type local:ImageButton}">
    <ControlTemplate.Resources>
        <Storyboard x:Key="MouseOverButton">
            <ThicknessAnimation Storyboard.TargetName="ButtonBackgroundBorder" 
                                Storyboard.TargetProperty="(Control.Margin)"
                                Duration="0:0:0.05" 
                                FillBehavior="Stop"
                                From="0,0,0,0" To="2,2,2,2" 
                                AutoReverse="True" />
        </Storyboard>
    </ControlTemplate.Resources>
    <Grid x:Name="ButtonOuterGrid">
        <Border x:Name="ButtonBackgroundBorder" 
                CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource TemplatedParent}}" 
                Background="{Binding Path=HighlightBackground, RelativeSource={RelativeSource TemplatedParent}}" 
                BorderBrush="Black"
                BorderThickness="0.8"
                Opacity="0">
            <Border.BitmapEffect>
                <OuterGlowBitmapEffect GlowColor="#FFFFFFFF" GlowSize="2.75" Noise="0.20"/>
            </Border.BitmapEffect>
        </Border>
        <Border x:Name="ButtonEdgesBorder" CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource TemplatedParent}}"
                Opacity="1" 
                BorderBrush="Transparent"
                BorderThickness="0" />
        <Border x:Name="ButtonContentBorder" 
                CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource TemplatedParent}}" 
                Opacity="1" 
                BorderThickness="1">
            <ContentPresenter x:Name="ContentText"
                              Width="Auto" Height="Auto"

                              HorizontalAlignment="Center" 
                              VerticalAlignment="Center"/>
        </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Trigger.Setters>
                <Setter Property="Opacity" TargetName="ButtonBackgroundBorder" Value="1"/>
                <Setter Property="TextElement.Foreground" TargetName="ContentText" Value="Black"/>
            </Trigger.Setters>
        </Trigger>
        <EventTrigger RoutedEvent="Grid.MouseEnter" 
                      SourceName="ButtonOuterGrid">
            <BeginStoryboard Storyboard="{StaticResource MouseOverButton}"/>
        </EventTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="ImageButton" TargetType="{x:Type Button}">
    <Setter Property="Template" Value="{StaticResource ButtonTemplate}" />
</Style>

这是最后一步,在xaml文件中你必须插入这个自定义控件

And this is last Step, in xaml file you must insert this custom-control

<ImageButton x:Name="btnConfigs"
                      Style="{StaticResource ImageButton}"
                      Width="25" Height="25"
                      VerticalAlignment="Top"
                      HorizontalAlignment="Right"
                      Margin="0,31.125,16.418,0">
            <Image x:Name="ImgConfigs"
                   Stretch="Fill"/>
        </ImageButton >

并在 cs 文件中执行此操作

and in cs file do this

 this.ImgConfigs.Source="any imag-source" 

我们也可以在 btnconfig-click 事件中更改此图像源

also we can change this image-source on btnconfig-click event

这篇关于图像按钮模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆