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


    1.界面模板代码部分

    1. <Window.Resources>
    2. <Style x:Key="IconButton" TargetType="Button">
    3. <Setter Property="Template">
    4. <Setter.Value>
    5. <ControlTemplate TargetType="Button">
    6. <Border x:Name="border"
    7. BorderBrush="{TemplateBinding BorderBrush}"
    8. BorderThickness="{TemplateBinding BorderThickness}"
    9. Background="{TemplateBinding Background}"
    10. Padding="{TemplateBinding Padding}"
    11. SnapsToDevicePixels="True"
    12. CornerRadius="8,8,8,8">
    13. <Grid>
    14. <Grid.ColumnDefinitions>
    15. <ColumnDefinition Width="Auto"/>
    16. <ColumnDefinition Width="*"/>
    17. </Grid.ColumnDefinitions>
    18. <Image Grid.Column="0" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(localModel:ButtonExtensions.IconPath)}" Width="16" Height="16" Margin="4,0"/>
    19. <ContentPresenter Grid.Column="1" x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}"
    20. Content="{TemplateBinding Content}"
    21. ContentStringFormat="{TemplateBinding ContentStringFormat}"
    22. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    23. VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    24. SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
    25. RecognizesAccessKey="True"
    26. Margin="{TemplateBinding Padding}"/>
    27. </Grid>
    28. </Border>
    29. <ControlTemplate.Triggers>
    30. <Trigger Property="IsMouseOver" Value="True">
    31. <Setter TargetName="border" Property="Background" Value="#7DB8FF"/>
    32. </Trigger>
    33. <Trigger Property="IsPressed" Value="True">
    34. <Setter TargetName="border" Property="Background" Value="#6CADFF "/>
    35. </Trigger>
    36. <Trigger Property="IsEnabled" Value="False">
    37. <Setter Property="Opacity" TargetName="border" Value="0.6"/>
    38. </Trigger>
    39. </ControlTemplate.Triggers>
    40. </ControlTemplate>
    41. </Setter.Value>
    42. </Setter>
    43. </Style>
    44. </Window.Resources>

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

    1. //通过附加属性来设置图标的路径
    2. public static class ButtonExtensions
    3. {
    4. public static readonly DependencyProperty IconPathProperty = DependencyProperty.RegisterAttached(
    5. "IconPath", typeof(string), typeof(ButtonExtensions), new PropertyMetadata(null));
    6. public static string GetIconPath(DependencyObject obj)
    7. {
    8. return (string)obj.GetValue(IconPathProperty);
    9. }
    10. public static void SetIconPath(DependencyObject obj, string value)
    11. {
    12. obj.SetValue(IconPathProperty, value);
    13. }
    14. }

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

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

  • 相关阅读:
    划分Vlan时需要注意的问题
    java-php-python-ssm网上拍卖系统2021计算机毕业设计
    [机缘参悟-73]:深度思考:心智提升的七个阶段
    软考 系统架构设计师系列知识点之设计模式(3)
    Java基础—循环栅栏CyclicBarrier
    开源CasaOS云软件发现关键漏洞
    Enzo 比色免疫酶免疫测定:蛋白A ELISA试剂盒
    CDGA|促进数据生产要素在大湾区自由流动,培养数据治理人才先行
    【华为OD机试python】返回矩阵中非1的元素个数【2023 B卷|200分】
    Vector和ArrayList的扩容
  • 原文地址:https://blog.csdn.net/Hat_man_/article/details/136675817