WPF制作带图标和文字的按钮模板(通过附加属性实现)

1.界面模板代码部分

    <Window.Resources>
        <Style x:Key="IconButton" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="border"
                            BorderBrush="{TemplateBinding BorderBrush}"  
                                BorderThickness="{TemplateBinding BorderThickness}"  
                                Background="{TemplateBinding Background}"  
                                Padding="{TemplateBinding Padding}"
                                SnapsToDevicePixels="True"
                                CornerRadius="8,8,8,8">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Image Grid.Column="0" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(localModel:ButtonExtensions.IconPath)}" Width="16" Height="16" Margin="4,0"/>
                                <ContentPresenter Grid.Column="1" x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}"   
                                      Content="{TemplateBinding Content}"   
                                      ContentStringFormat="{TemplateBinding ContentStringFormat}"   
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"   
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"   
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"   
                                      RecognizesAccessKey="True"   
                                      Margin="{TemplateBinding Padding}"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="Background" Value="#7DB8FF"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="border" Property="Background" Value="#6CADFF "/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" TargetName="border" Value="0.6"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

2.在工程中添加类,用于编辑附加属性,主要是为了设置图标地址。下面为附加属性代码

//通过附加属性来设置图标的路径
    public static class ButtonExtensions
    {
        public static readonly DependencyProperty IconPathProperty = DependencyProperty.RegisterAttached(
            "IconPath", typeof(string), typeof(ButtonExtensions), new PropertyMetadata(null));

        public static string GetIconPath(DependencyObject obj)
        {
            return (string)obj.GetValue(IconPathProperty);
        }

        public static void SetIconPath(DependencyObject obj, string value)
        {
            obj.SetValue(IconPathProperty, value);
        }
    }

3.界面按钮代码实现代码,如下

<Button Grid.Row="1" Grid.Column="0" x:Name="BtnLoadImage" 
Style="{StaticResource IconButton}" 
localModel:ButtonExtensions.IconPath="pack://application:,,,/Resources/OpenPath.png" 
Width="100" Height="40" 
Content="Load Image" 
Background="#6CADFF" BorderBrush="Transparent"/>

4.如果想要从后台代码修改按钮上的图片,则代码如下

ButtonExtensions.SetIconPath(myButton, "pack://application:,,,/Resources/NewImage.png" );

  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承 WPF 中的 Button 类并实现 ICommandSource 接口来创建一个自定义控件,该控件可以有参数命令。首先,创建一个类,继承 Button 类并实现 ICommandSource 接口,如下所示: ```csharp public class CommandButton : Button, ICommandSource { public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register( "CommandParameter", typeof(object), typeof(CommandButton), new PropertyMetadata(default(object))); public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof(ICommand), typeof(CommandButton), new PropertyMetadata(default(ICommand))); protected override void OnClick() { if (Command != null) { Command.Execute(CommandParameter); } base.OnClick(); } } ``` 在这个自定义控件中,我们定义了一个名为 CommandButton 的类,其中包含一个名为 CommandParameter 的依赖属性和一个名为 Command 的依赖属性。我们还重写了 OnClick 方法,在单击按钮时执行与命令相关的操作。 现在,您可以在 XAML 中使用这个自定义控件,并为 Command 和 CommandParameter 属性设置值,如下所示: ```xml <local:CommandButton Content="Click Me!" Command="{Binding MyCommand}" CommandParameter="parameter" /> ``` 其中,local 是指您的自定义控件所在的命名空间。MyCommand 是您的 ViewModel 中实现的命令,parameter 是该命令的参数。当用户单击按钮时,命令将被执行,并且参数将被传递给命令的 Execute 方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值