代码之家  ›  专栏  ›  技术社区  ›  Rev

图像按钮模板?

wpf
  •  0
  • Rev  · 技术社区  · 14 年前

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

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

    3 回复  |  直到 14 年前
        1
  •  4
  •   Oskar    14 年前

    为什么这个图像按钮必须是用户控件?如果带有新控件模板的常规按钮正常,则应该可以:

    <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>
    
        2
  •  0
  •   Shoaib Shaikh    14 年前

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

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

        3
  •  0
  •   Rev    14 年前

    我在代码项目文章中找到了这个(很酷的例子)

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

    首先,他创建WPF自定义控件(您可以像这样创建从按钮继承的类)

     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" 
    

    此外,我们还可以在bnconfig click事件上更改此图像源

    特别感谢Murray Foxcroft撰写了这篇文章。