具有自定义内容模板的 WPF 创建按钮

2023-12-05

我在 WPF 中有一个应用程序,需要创建许多具有相同内容布局的按钮。目前它在 Window 中定义为:

<Button Grid.Row="0" Grid.Column="0" Margin="4" >
    <Button.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.85*" />
                <RowDefinition Height="0.25*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" TextAlignment="Center" Text="Primary Text that can wrap" TextWrapping="Wrap" FontSize="14.667" />
            <TextBlock Grid.Row="1" TextAlignment="Left" Text="smaller text" FontSize="10.667" />
        </Grid>
    </Button.Content>
</Button>

我理想的做法是将其更改为:

<controls:MultiTextButton Grid.Row="0" Grid.Column="0" PrimaryText="Primary Text that can wrap" SecondaryText="smaller text" />

无论正确还是错误,我创建了以下类:

public class MultiTextButton : Button
{
    public static readonly DependencyProperty PrimaryTextProperty = DependencyProperty.Register("PrimaryText", typeof(String), typeof(MultiTextButton));

    public static readonly DependencyProperty SecondaryTextProperty = DependencyProperty.Register("SecondaryText", typeof(String), typeof(MultiTextButton));

    static MultiTextButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiTextButton), new FrameworkPropertyMetadata(typeof(MultiTextButton)));
    }

    public string PrimaryText
    {
        get { return (string)GetValue(PrimaryTextProperty); }
        set { SetValue(PrimaryTextProperty, value); }
    }

    public string SecondaryText
    {
        get { return (string)GetValue(SecondaryTextProperty); }
        set { SetValue(SecondaryTextProperty, value); }
    }
}

我现在不确定如何设置“模板”以按照窗口中原始代码的格式显示内容。我试过了:

<ControlTemplate x:Key="MultiTextButtonTemplate" TargetType="{x:Type controls:MultiTextButton}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.85*" />
            <RowDefinition Height="0.25*" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" TextAlignment="Center" Text="{Binding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
        <TextBlock Grid.Row="1" TextAlignment="Left" Text="{Binding SecondaryText}" FontSize="10.667" />

    </Grid>
</ControlTemplate>

但在 Blend 和 Visual Studio 中,按钮不会呈现。


你在追寻TemplateBinding:

<TextBlock Grid.Row="0" TextAlignment="Center" Text="{TemplateBinding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
<TextBlock Grid.Row="1" TextAlignment="Left" Text="{TemplateBinding SecondaryText}" FontSize="10.667" />

此绑定专门用于控件模板——它引用控件中的依赖属性。 (请注意,相比之下,常规绑定指的是当前的属性DataContext.)


Edit

要使其看起来像一个按钮,请复制默认控制模板Button并替换其ContentPresenter如下所示:

<ContentControl Margin="2"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                RecognizesAccessKey="True"
                ContentTemplate="{TemplateBinding ContentTemplate}">
    <ContentControl.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.85*" />
                <RowDefinition Height="0.25*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" TextAlignment="Center" Text="{Binding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
            <TextBlock Grid.Row="1" TextAlignment="Left" Text="{Binding SecondaryText}" FontSize="10.667" />

        </Grid>
    </ContentControl.Content>
</ContentControl>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有自定义内容模板的 WPF 创建按钮 的相关文章

随机推荐

  • 需要帮助防止无限循环。属性设置

    所以我有一个颜色选择器 用户可以使用 RGB 或 HSB 选择颜色 每个值都有滑块 属性 例如 当用户设置红色时 我将计算 HSB 值以反映新的颜色值 当用户设置 Hue 时 RGB 值将从 HSB 值重新计算 但请注意那里有一个循环 当我
  • Azure AD B2C:User.Identity.Name 为 null,但 User.Identity.m_instance_claims[9] 具有名称

    用户通过我的 Azure AD B2C Web 应用程序身份验证后 我尝试检索User Identity Name 然而 它是空的 然而 User Identity m instance claims 9 如下面的屏幕截图所示 确实具有正确
  • 如何以编程方式隐藏选项卡栏,然后展开视图以适合

    我从这个问题中得到了代码 如何以编程方式隐藏 UITabBarController 这很棒 但是视图现在无法扩展以适应选项卡栏留下的空间 我已经为视图设置了适当的 UIViewAutoresizingMasks 但我假设仅仅因为它的隐藏并不
  • 如何在文本按钮上放置图标?

    我想在同一个按钮上同时显示图像图标和文本 例如在 Word 中 我在按钮上设置了图标 但文本消失了 HANDLE hBmp HBITMAP LoadImage g hDllInstance MAKEINTRESOURCE IDB BITMA
  • 如何在 Spark 中设置 ORC 条带大小

    我正在尝试在 Spark 2 3 中生成数据集并以 ORC 文件格式编写 我正在尝试设置 ORC 条带大小和压缩大小的一些属性 我从中得到了暗示this所以帖子 但 Spark 不尊重这些属性 并且生成的 ORC 文件中的条带大小比我设置的
  • PHP:如何识别并更改数组中的重复值?

    好的 在 php 数组中有很多重复检测和删除的示例 使用 array unique 等 但是如果您想查找重复项 修改它们 再次循环检查直到所有重复项现在都是唯一的 该怎么办 我认为这就像使用 array filter 所以作为一个更具体的示
  • TFS:构建具有多种配置的解决方案

    设想 在 TFS 2013 上 我必须构建 300 多个项目 C 和 VC 分为大约 40 个解决方案 有些项目有多个发布和调试配置 例如一个项目可能有 3 个发布配置 如版本 1 版本 2 版本 3 x86 版本 4 x64 等 要求 我
  • 这是使用 Python 3 unittest 测试 stdout 的正确方法吗?

    假设我有一个提交文件 fileFromStudent py 其中唯一的内容是 print hello world 我想测试标准输出 看看学生是否正确写出了打印语句 根据我所读到的内容 我已经能够创建以下代码 from io import S
  • 序列化多态接口

    我希望从其关联的接口序列化一个多态类 这是我发现的这个问题 它似乎做了我需要做的事情 如何在Boost Serialization中创建序列化接口 然而 序列化是从类本身而不是接口完成的 到目前为止我得到了什么 网络消息 hpp using
  • 可以知道变量所在的内存部分吗?

    C 程序中有什么方法可以知道变量所在的部分吗 例如 char str Word1 char str2 Word2 printf String1 s Location p n str str lt would be on the stack
  • java 如何打印经过的时间(秒)

    在游戏循环的 run 方法中 我尝试打印程序在 java 中运行的时间 我只是尝试过System out println System nanoTime 1000000 因为这就是一秒有多少毫秒 如果您不知道 它会打印接近尾声的秒数 但我想
  • 角度2错误httpclientmodule

    have some error with httpclientmodule i have app module ts and there is code import NgModule from angular core import Br
  • SVG 填充颜色透明度/alpha?

    是否可以在 SVG 填充颜色上设置透明度或 Alpha 级别 我尝试向填充标签添加两个值 将其从fill 044B94 to fill 044B9466 但这不起作用 您使用附加属性 fill opacity 该属性采用 0 0 到 1 0
  • Windows Phone 键盘打开事件和属性

    在我的 Windows Phone 应用程序上 我需要根据键盘更改视图 我有几个问题 如何判断键盘是否打开 是否有键盘打开的视图事件 有没有办法获得键盘的高度 或者被阻止的 UI 区域大小 通过键盘 您可以通过以下方式访问键盘信息Windo
  • 使用 SWIG 作为参数传递给 C 库的错误值

    跟随我的three previous posts 我现在可以将托管结构数组传递给我的包装方法 以下是文件的摘录 packer i typedef struct int width input int height input frame t
  • VBA Excel/Word 查找和替换

    我正在开发一个 Excel 工作表 用于在 Word 文档中搜索特定实例 A 列 并将其替换为单元格 B 中的实例 我只想更改与搜索条件匹配的第一个实例 并继续循环该列到下一个实例 我写了下面的代码 如果我使用 wdReplaceAll 它
  • Rstudio 控制台中的命令行错误

    如何将一个很长的字符串分配给r中的变量 以下工作正常 testVar lt test test 但以下给出了一个奇怪的 在 RStudio 控制台中 testVar lt test test test test test test test
  • 检索 x509 证书的序列号时缺少前导零

    我正在尝试从 X 509 证书获取序列号 当我将代码生成的序列号与实际序列号 在 Windows 上 进行比较时 实际序列号 X509 证书 的前导零是丢失的 有任何建议或替代方法来获取带有前导零的十六进制 x 509 证书的序列号吗 以下
  • MVC 异步错误 - 异步操作方法“Complete”无法同步执行

    我正在将 MVC4 与 VS 2010 一起使用 我有一个正在尝试运行的异步操作 我的控制器继承自 AsyncController 并且我有 Async 和 Completed 方法 我能够在示例测试项目中正确执行异步操作 但是当我将它作为
  • 具有自定义内容模板的 WPF 创建按钮

    我在 WPF 中有一个应用程序 需要创建许多具有相同内容布局的按钮 目前它在 Window 中定义为