【问题标题】:image button template?图片按钮模板?
【发布时间】:2010-04-14 06:37:56
【问题描述】:

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

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

【问题讨论】:

  • 在您的问题和您对答案的一些回复中,您提出了几个关于需要用户控件的问题,但您没有解释您的意思。从您关于制作软件电话的评论看来,您需要一个模板,而不是用户控件。请举例说明你想写什么,也许我们可以帮助你。一些假设的 XAML 会很好。换句话说,我建议你的框架为“我想写一些看起来像这样的东西:_________,我该怎么做?”
  • 我在 1 个月前解决了我的问题。我会发送解决方案

标签: wpf


【解决方案1】:

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

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

【讨论】:

  • 这个解决方案对我不好。如果我想使用你的解决方案,我需要在我的 xaml 中制作 30 张图像。(15 按钮)
  • 然后更详细地解释你想如何使用这个用户控件。它不是那么明显。
  • 您可以将新的控件模板放在一个样式中,并将其用于您的 15 个按钮
  • 我想用 wpf 制作一个软件电话。我想用那个用户控件来控制数字按钮和其他一些按钮(配置按钮,拨号按钮)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我在 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 撰写这篇文章

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2011-09-08
      • 2012-07-04
      • 2012-05-29
      相关资源
      最近更新 更多