WPF C# 按钮样式

2023-12-26

有人知道如何在 WPF 中重新创建此按钮样式吗?因为我不知道如何制作不同的隔间。以及两种不同的文本和文本样式?


要解决你的问题肯定需要使用Style and Template为了Button。但他到底长什么样呢?决定可能有几个。例如,Button两个文本可以更好地定义相关内容TextBlocks?可以直接在模板中使用,但是接下来使用的按钮就会受到限制,因为模板中只能有一个ContentPresenter。我决定以不同的方式做事,找出一个ContentPresenter带有 a 形式的图标Path,并使用侧面的按钮设置内容。

样式:

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#373737" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontSize" Value="15" />
    <Setter Property="SnapsToDevicePixels" Value="True" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border CornerRadius="4" Background="{TemplateBinding Background}">
                    <Grid>
                        <Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="17,0,0,0" Data="F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z "/>
                        <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" />                                
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#E59400" />
                        <Setter Property="Foreground" Value="White" />
                        <Setter TargetName="PathIcon" Property="Fill" Value="Black" />
                    </Trigger>

                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="OrangeRed" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

使用示例:

<Button Width="200" Height="50" VerticalAlignment="Top" Margin="0,20,0,0" />
    <Button.Content>
        <StackPanel>
            <TextBlock Text="Watch Now" FontSize="20" />
            <TextBlock Text="Duration: 50m" FontSize="12" Foreground="Gainsboro" />
        </StackPanel>
    </Button.Content>
</Button>

Output

最好是StackPanel确定Resources并设置Button so:

<Window.Resources>
    <StackPanel x:Key="MyStackPanel">
        <TextBlock Name="MainContent" Text="Watch Now" FontSize="20" />
        <TextBlock Name="DurationValue" Text="Duration: 50m" FontSize="12" Foreground="Gainsboro" />
    </StackPanel>
</Window.Resources>

<Button Width="200" Height="50" Content="{StaticResource MyStackPanel}" VerticalAlignment="Top" Margin="0,20,0,0" />

问题仍然在于设置值TextBlock Duration,因为这个值必须是dynamic。我使用附件实现了它DependencyProperty。将其设置为窗口,如下所示:

<Window Name="MyWindow" local:MyDependencyClass.CurrentDuration="Duration: 50m" ... />

使用于TextBlock:

<TextBlock Name="DurationValue" Text="{Binding ElementName=MyWindow, Path=(local:MyDependencyClass.CurrentDuration)}" FontSize="12" Foreground="Gainsboro" />

事实上,对于任何人来说,确定所附的内容并没有什么区别。DependencyProperty,因为它是主要特征。

设定值示例:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyDependencyClass.SetCurrentDuration(MyWindow, "Duration: 101m");
}

完整的示例列表:

XAML

<Window x:Class="ButtonHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ButtonHelp"
    Name="MyWindow"
    Title="MainWindow" Height="350" Width="525"
    WindowStartupLocation="CenterScreen"
    local:MyDependencyClass.CurrentDuration="Duration: 50m">

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#373737" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="FontFamily" Value="./#Segoe UI" />
        <Setter Property="SnapsToDevicePixels" Value="True" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border CornerRadius="4" Background="{TemplateBinding Background}">
                        <Grid>
                            <Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="17,0,0,0" Data="F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z "/>
                            <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" />                                
                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#E59400" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter TargetName="PathIcon" Property="Fill" Value="Black" />
                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="OrangeRed" />
                            <Setter Property="Foreground" Value="White" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <StackPanel x:Key="MyStackPanel">
        <TextBlock Name="MainContent" Text="Watch Now" FontSize="20" />
        <TextBlock Name="DurationValue" Text="{Binding ElementName=MyWindow, Path=(local:MyDependencyClass.CurrentDuration)}" FontSize="12" Foreground="Gainsboro" />
    </StackPanel>
</Window.Resources>

<Grid>        
    <Button Width="200" Height="50" Content="{StaticResource MyStackPanel}" VerticalAlignment="Top" Margin="0,20,0,0" />

    <Button Content="Set some duration" Style="{x:Null}" Width="140" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Click="Button_Click" />
</Grid>

Code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyDependencyClass.SetCurrentDuration(MyWindow, "Duration: 101m");
    }
}

public class MyDependencyClass : DependencyObject
{
    public static readonly DependencyProperty CurrentDurationProperty;        

    public static void SetCurrentDuration(DependencyObject DepObject, string value)
    {
        DepObject.SetValue(CurrentDurationProperty, value);
    }

    public static string GetCurrentDuration(DependencyObject DepObject)
    {
        return (string)DepObject.GetValue(CurrentDurationProperty);
    }

    static MyDependencyClass()
    {
        PropertyMetadata MyPropertyMetadata = new PropertyMetadata("Duration: 0m");

        CurrentDurationProperty = DependencyProperty.RegisterAttached("CurrentDuration",
                                                            typeof(string),
                                                            typeof(MyDependencyClass),
                                                            MyPropertyMetadata);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF C# 按钮样式 的相关文章

  • 使 DataTemplate 可混合

    如何为 ViewModel 制作可混合的数据模板 可在表达式混合中设计 当我转到资源并尝试直接编辑数据模板时 我在绘图板上看到的只是一个空白矩形 这是因为 DataTemplate 没有绑定到任何东西 当然 我可以创建一个 UserCont
  • 窗口关闭后仍在调用方法

    首先我不知道这是不是一个愚蠢的问题 我有这样的场景 首先我有一个主窗口 public MainWindow InitializeComponent dt is a System Windows Threading DispatcherTim
  • 如何在C#背后的代码中动态创建数据模板并绑定TreeView分层数据

    我有一个场景 其中树视图动态更改其数据模板和数据绑定定义 我在 XAML 中创建了一个树视图 如下所示
  • Winforms 中的 WPF ElementHost 最大化时崩溃 (Windows)

    我正在尝试将新的 WPF 控件集成到现有的 WinForms 应用程序中 并使用 ElementHost Dock Fill 来托管以下 XAML UserControl NET 4 当我将 WinForm 设置为最大化时 我的整个操作系统
  • Web 客户端和 Expect100Continue

    使用 WebClient C NET 时设置 Expect100Continue 的最佳方法是什么 我有下面的代码 我仍然在标题中看到 100 continue 愚蠢的 apache 仍然抱怨 505 错误 string url http
  • 为 Angular2 中的组件加载多个样式表

    我正在构建一个 angular2 应用程序 当我尝试为同一组件加载多个样式表时 我面临多个问题 这是我正在做的代码 import Component from angular core Component selector my tag t
  • 带动态元素的 WPF 启动屏幕。如何?

    我是 WPF 新手 我需要一些帮助 我有一个加载缓慢的 WPF 应用程序 因此我显示启动屏幕作为权宜之计 但是 我希望能够在每次运行时更改屏幕 并在文本区域中显示不同的引言 这是一个生产力应用程序 所以我将使用非愚蠢但激励性的引言 当然 如
  • WPF/C# 将自定义对象列表数据绑定到列表框?

    我在将自定义对象列表的数据绑定到ListBox in WPF 这是自定义对象 public class FileItem public string Name get set public string Path get set 这是列表
  • 从 FileReader 设置背景图像样式

    我正在寻找一种解决方案 允许我从文件上传输入中获取文件并通过设置 document body style backgroundImage 来预览它 以下代码用于在 Image 元素中显示预览 function setImage id tar
  • WPF ListView/Gridview 允许用户选择多个项目并将它们分组在一起

    我在 MVVM 应用程序中有一个 WPF ListView GridViwe GridView 绑定到 ViewModel 中的列表 要求是用户应该能够选择网格视图的多行 右键单击它并看到上下文菜单 将这些行组合在一起 选择后 所有这些项目
  • 给图像着色[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试着色System Windows Controls Image 该图像包含透明区域 我只是想用颜色给非透明区域着色 例如 图
  • WPF控件默认大小

    为 wpf 应用程序定义自定义资源主题时 我可以设置宽度 高度等 如何找到这些属性的默认值 即框架中提供的控件中使用的值 WPF 控件通常不包含任何类型的默认大小 WPF 的主要功能点之一是 除非您指定大小 否则所有内容都会动态调整大小 如
  • Visual Studio 扩展找不到所需的程序集

    我为 Visual Studio 2013 编写了一个扩展 因为该死的组合框错误 https stackoverflow com questions 7800032 cancel combobox selection in wpf with
  • WPF 创建同级窗口并关闭当前窗口

    我需要的是我的窗口类中的这样一个事件处理程序 void someEventHandler object sender RoutedEventArgs e MyNewWindow mnw new MyNewWindow mnw Owner W
  • 将 BitmapImage 从 Resources.resx 分配给 Image.Source?

    我想分配一个BitmapImage从我的 Resources resx 到Image 之前我将 png 图像保存到 Resources resx 中 该图像现在位于 Resources logo png 中 在阅读了几篇 SO 帖子和其他文
  • Windows Phone 8.1 ComboBox 在项目数量较多时显示多个项目

    当组合框中有更多项目时 组合框将显示列表选择器弹出按钮 如果我选择第一个并向下滚动 则多个项目将显示为选定的 但 ComboBox 的 SelectedItem 将是我选择的项目 我修改了ListPickerFlyout的样式 并关闭了Li
  • 在 NUnit 测试中使用 WPF 组件 - 如何使用 STA?

    我需要在 NUnit 单元测试中使用一些 WPF 组件 我通过 ReSharper 运行测试 在使用 WPF 对象时失败并出现以下错误 系统 InvalidOperationException 调用线程必须是 STA 因为许多 UI 组件都
  • WPFToolkit.Extended - 工具在哪里?

    我已采取的步骤 我正在使用 NET 4 0 我得codeplex http wpftoolkit codeplex com releases view 84227并下载了第一个 扩展 WPF 工具包二进制文件 2 号将此文件 Extende
  • 计算 Richtextbox 中所有单词的最有效方法是什么?

    我正在编写一个文本编辑器 需要提供实时字数统计 现在我正在使用这个扩展方法 public static int WordCount this string s s s TrimEnd if String IsNullOrEmpty s re
  • 将div设置为隐藏,延时后可见

    我试图在 X 时间后 也许甚至在随机时间之后 但现在我们只做固定时间 在黑色背景上出现一个黄色方块 function initialSetup if document getElementById yellow null document

随机推荐

  • 在每次循环迭代中创建对象

    我正在使用内置的 z80 导航控件 以下是演示链接 Z80 导航菜单 https github com kernelENREK Z80 NavigationBar 如果有人看到该控件 它有一个对象来创建菜单 如父菜单 及其下的子菜单 像下面
  • Android Studio 中的“noinspection”关键字列表

    在 Android Studio 中 我有时会使用这种注释来禁用对我已经处理过的特定代码行的检查警告 例如 if Build VERSION SDK INT lt Build VERSION CODES JELLY BEAN noinspe
  • 添加悬停效果,例如一个(大)绘图表的工具提示?

    我是编程新手 我对 R 的了解还算丰富 我正在努力生成一个大表格 其中单元格内容将从将鼠标光标悬停在相关单元格上展开 这与另一个问题中的提议类似 在闪亮的数据表中为每个单元格显示工具提示或弹出窗口 https stackoverflow c
  • Vim 光标用 .vimrc 换行

    我似乎无法让光标换行在 vim 7 3 中工作 我尝试过其他地方找到的建议 包括以下建议 但没有效果 set whichwrap lt gt set whichwrap gt l set whichwrap lt h 有什么建议么 我已经包
  • 如何在 Angular 中嵌入 YouTube 视频?

    我在 YouTube 上关注了这个教程 它基本上是一张包含您喜欢的音乐的表格 但教程结束了 它使用 Angular2 一切都工作正常 但是这位先生留下的地方 它只是使用以下代码在控制台中显示视频的构造函数 播放列表 组件 Ts export
  • Apache Velocity:如何像 Java 中那样定义自定义方法?

    我目前在 Polarion 应用程序 ALM 软件中编写了一个 wiki 页面 这些页面上的语法包括 据我所知 HTML Javascript 和 Apache Velocity 所以我想在 Apache Velocity 中编写一个脚本
  • 如何使用Jest测试文件下载?

    我有一些代码如下 global document global window global Blob import FileSaver from file saver export const createDownloadFromBlob
  • 检测数字键盘是否存在?

    是否可以判断系统是否连接有数字键盘 台式机键盘通常有数字键盘 而笔记本电脑通常没有 尽管它们将数字键盘键放在普通键盘中 并通过 Num Lock 激活 操作系统是否知道小键盘按键是否在物理上是分开的 如果是这样 这些信息是否会以某种方式暴露
  • 通过 ODBC 连接检索的表情符号数据显示为问号

    我从经典 ASP 页面连接到 MySQL 版本 5 5 50 log MySQL数据库设置如下 数据库字符集 utf8mb4 数据库排序规则 utf8mb4 general ci 表和字段 字符集 utf8mb4 排序规则 utf8mb4
  • 更新 mnesia 架构的最简单方法是什么?

    例如 我保存了 id name 在 mnesia 并想要更新到 id name age 我需要打电话吗transform table每次我更改架构时 最简单的方法是删除该表并重新创建它 如果需要保留表中的数据 mnesia transfor
  • 来自字符串“Yellow”的 Java 颜色

    有什么方法可以从字符串中获取颜色 例如 白色 Color color Field field Class forName java awt Color getField Yellow color Color field get null I
  • 动态添加的表单字段在 form.cleaned_data 中被删除

    我在模板中放置了一些客户端 JavaScript 允许用户动态地将字段添加到表单中 我的问题是这些字段被清理了form cleaned data 所以我无法以这种方式访问 它们 所有字段都可以在request POST 所以我可以用它来解决
  • 算法 - 如何通过 2n/3 比较对 0/1 数组进行排序?

    In 算法设计手册 http www algorist com 有这样的消费税 4 26 考虑使用以下方法对 n 个 0 和 1 的序列进行排序的问题 比较 对于两个值 x 和 y 的每次比较 该算法 了解 x y 中哪一个成立 a 给出一
  • 行构造函数有什么用?

    在 PostgreSQL 中 什么是ROW 函数用于 具体有什么区别 SELECT ROW t f1 t f2 42 FROM t where f1属于类型int f2属于类型text and CREATE TYPE myrowtype A
  • 查询DB并返回Json格式结果的通用Java方法

    我正在寻找的东西对我来说似乎很简单 但我的护目镜失败了 我想要一个允许我运行任何查询并获取 json 格式结果的方法 诀窍是我don t希望结果需要 java 对象作为过程的一部分 DTO VO 等 有什么快速 简单 干净的方法可以做到这一
  • 如何在xsd文件中定义xml主键并在xml文件中验证

    建筑物 xsd
  • 在 Android 的警报管理器中使用 Intent Extras 传递值

    我想知道这段代码是否有效 我现在无法尝试 此外 我还有一些疑问需要澄清 Intent intent new Intent context AlarmReceiver class intent putExtra user global get
  • 如何从R中的数据帧创建不同格式的矩阵?

    我的数据框如下 group id user id 1000 26 1236 29 1236 46 3767 26 3767 46 5614 29 5614 45 5614 46 我需要输出如下 User 1 User 2 of common
  • 将图像从 3d 视角重绘为 2d

    我需要用 Pascal Delphi Lazarus 编写的逆透视变换 请参见下图 我想我需要遍历目标像素 然后计算源图像中的相应位置 以避免舍入误差等问题 function redraw 3d to 2d sourcebitmap tbi
  • WPF C# 按钮样式

    有人知道如何在 WPF 中重新创建此按钮样式吗 因为我不知道如何制作不同的隔间 以及两种不同的文本和文本样式 要解决你的问题肯定需要使用Style and Template为了Button 但他到底长什么样呢 决定可能有几个 例如 Butt