图像按钮模板?

时间:2010-04-14 06:37:56

标签: wpf

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

解决方案是使用带有“值转换器”的模板,但我不知道怎么做?

3 个答案:

答案 0 :(得分:4)

为什么此图像按钮必须是用户控件?如果带有新控件模板的常规按钮没问题,这应该可行:

<Button>
  <Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
      <Grid>
        <Image Name="HoverImage" Source="hover_image.png" Visibility="Hidden" />
        <Image Name="DefaultImage" Source="default_image.png" />
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter TargetName="DefaultImage" Property="Visibility" Value="Hidden" />
          <Setter TargetName="HoverImage" Property="Visibility" Value="Visible" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Button.Template>
</Button>

答案 1 :(得分:0)

如果你需要一个简单的翻转效果,你不需要一个模板..下面的文章有一个解决方案..

http://www.c-sharpcorner.com/Resources/Detail.aspx?ResourceId=706 在本文中,用户使用SolidColorBrush,您可以使用ImageBrush将图像设置为按钮的背景。

答案 2 :(得分:0)

我在Code-project-Article(酷示例)

上找到了这个

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

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

 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; }
    }
}

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

<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文件中,您必须插入此自定义控件

<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文件中执行此操作

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

我们也可以在btnconfig-click事件

上更改此图像源

特别感谢Murray-Foxcroft撰写的文章