wpf - 如何使用自定义启用/禁用图像创建一个简单的 WPF UserControl 按钮?

标签 wpf image user-controls button

我对 WPF 和 XAML 有点陌生,现在才学习。

我发现了一些以前的开发人员的快速脏代码:

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
        <Border Name="buttonBorder" Background="{TemplateBinding Background}">
            <Border.Effect>
                <DropShadowEffect Opacity="0.0" />
            </Border.Effect>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="buttonBorder" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect Opacity="0.8" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsMouseCaptured" Value="true">
                    <Setter TargetName="buttonBorder" Property="Effect">                   
                         <Setter.Value>
                        <DropShadowEffect Opacity="0.8" Direction="135"  
                             ShadowDepth="3" BlurRadius="1" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                   <Setter TargetName="buttonBorder" Property="Background">
                        <Setter.Value>
                        <ImageBrush ImageSource="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}"  />
                        </Setter.Value>
                    </Setter>            
                   <Setter TargetName="buttonBorder" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect Opacity="0.0"/>
                    </Setter.Value>
                </Setter>     

            </Trigger>
        </ControlTemplate.Triggers>
        </ControlTemplate>

基本上它只是一个具有基本鼠标悬停效果的按钮模板和绑定(bind)到标签的禁用状态图像(似乎是一个丑陋的解决方案)。

我想创建一个几乎相同的自定义按钮,但我想公开两个自定义属性:NormalImage 和 DisabledImage。这些属性应该是字符串类型而不是 Uri。我想使用图像的路径只是“apply.png”而不是“pack://application:,,,/Resources/Apply.png”。
我想,要拥有这样的自定义属性,我需要一个具有依赖属性的 UserControl?

基本上,我想按如下方式使用按钮:
<MyImageButton NormalImage="apply.png" DisabledImage="apply_disabled.png"/>

也许 NormalImage/DisabledImage 稍后会绑定(bind)到某些东西,但这不太可能。

我找不到任何实现具有自定义属性的基本按钮的示例,在线只有一些花哨的按钮和控件。也许我只是使用了不正确的关键字......

谁能指出我正确的文章或抛出一些简单的代码来玩?

WPF对于初学者来说太复杂了,有时它只是无法按预期工作,例如我仍然不明白为什么我可以将Trigger标签添加到ControlTemplate,但我不能将Trigger标签直接添加到UserControl......

最佳答案

您也可以使用 UserControl这有点困惑,因为您的按钮将被封装,看起来像这样:

xml:

<UserControl x:Class="Test.ImageButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Button Name="button" Click="button_Click" Width="50" Height="50">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="buttonBorder">
                    <Border.Effect>
                        <DropShadowEffect Opacity="0.0" />
                    </Border.Effect>
                    <Border.Child>
                        <Image Name="img" Source="{Binding NormalImage}"/>
                    </Border.Child>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="buttonBorder" Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect Opacity="0.8" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsMouseCaptured" Value="true">
                        <Setter TargetName="buttonBorder" Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect Opacity="0.8" Direction="135"  
                             ShadowDepth="3" BlurRadius="1" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="img" Property="Source" Value="{Binding DisabledImage}"/>
                        <Setter TargetName="buttonBorder" Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect Opacity="0.0"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>
</UserControl>

后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test
{
    /// <summary>
    /// Interaction logic for ImageButton.xaml
    /// </summary>
    public partial class ImageButton : UserControl
    {
        public ImageSource DisabledImage
        {
            get { return (ImageSource)GetValue(DisabledImageProperty); }
            set { SetValue(DisabledImageProperty, value); }
        }
        public static readonly DependencyProperty DisabledImageProperty =
            DependencyProperty.Register("DisabledImage", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));


        public ImageSource NormalImage
        {
            get { return (ImageSource)GetValue(NormalImageProperty); }
            set { SetValue(NormalImageProperty, value); }
        }
        public static readonly DependencyProperty NormalImageProperty =
            DependencyProperty.Register("NormalImage", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));

        public event RoutedEventHandler Click;

        public ImageButton()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }
    }
}

示例用法:
        <local:ImageButton x:Name="imgbutton"
                           NormalImage="C:/1.png"
                           DisabledImage="C:/2.png"
                           Click="ImgButton_Click"/>

(请注意,当前的命名空间是 Test ,您可能想要更改它;另外我在内部按钮上设置了一个固定大小,您可能想要删除它,只要确保在某处设置大小,因为我认为它不会如果您不这样做,请使用任何空间。)

关于wpf - 如何使用自定义启用/禁用图像创建一个简单的 WPF UserControl 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4984917/

相关文章:

c# - WPF 的 C# 代码中的 XPath

wpf - 如何清除 MVVM 中的文本框?

image - 使用 <h :graphicImage> or <img> tag 从 webapps/webcontext/deploy 文件夹外部加载图像

linux - linux中的作业调度

c# - 自定义用户控件未在自动生成的代码中初始化

asp.net - 如何从 asp :content page using the master? 访问母版页上的用户控件

wpf - 为什么绑定(bind)对目标到源不起作用

javascript - 在kineticjs中创建变量

java - 如何在 javafx 中调整 imageview 图像的大小?

c# - WPF DispatcherFrame 魔术 - 这是如何以及为什么起作用的?