让模板化按钮的命令发挥作用

发布于 2024-08-11 01:05:22 字数 491 浏览 1 评论 0原文

我使用控件模板以简单的方式更改按钮的外观。现在它看起来有所不同,但其行为并不像按钮。实际上有两个问题:

  1. 按钮的命令永远不会被执行
  2. 单击按钮后,它显示为被选中(即,椭圆变成丑陋的蓝色矩形)

这是一般想法:

<Button Command="{x:Static commands:...}"
        CommandParameter="{Binding}">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Ellipse Fill="{Binding ...}"
                     .../>
        </ControlTemplate>
    </Button.Template>
</Button>

I've used a control template to change the appearance of a button in a trivial way. It now looks different, but does not behave like a button. There are really two problems:

  1. The button's command is never executed
  2. After clicking on the button, it appears selected (i.e., the ellipse turns into an ugly blue rectangle)

Here's the general idea:

<Button Command="{x:Static commands:...}"
        CommandParameter="{Binding}">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Ellipse Fill="{Binding ...}"
                     .../>
        </ControlTemplate>
    </Button.Template>
</Button>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一城柳絮吹成雪 2024-08-18 01:05:22
  1. 没有理由发生这种情况。我使用 ApplicationCommands.Copy 进行了一次测试,并且命令触发得很好。可能是您的 CommandBinding 无法正常工作。
  2. 基于复制示例 XAML 并仅设置 Fill="Green",我也没有看到这一点。您可以尝试在 Button 上设置 FocusVisualStyle="{x:Null}"
  1. There's no reason this should be happening. I put together a test using ApplicationCommands.Copy and the command fired just fine. Could be your CommandBinding isn't working properly.
  2. I also didn't see this based on copying your sample XAML and just setting Fill="Green". You can try setting FocusVisualStyle="{x:Null}" on the Button.
画▽骨i 2024-08-18 01:05:22

问题原来是 Fill 绑定到了一个可能为 null 的值。如果Fill画笔为空而不是透明,则无需单击任何内容,并且不会执行该命令。正如德鲁提到的,使用实心填充时,按钮可以正常工作。

要点:如果您想隐藏形状但仍让它响应用户交互,请使用透明画笔,而不是空画笔。

The problem turned out to be that Fill was bound to a value that could be null. If the Fill brush is null rather than transparent, then there's nothing to click and the command doesn't get executed. As Drew mentioned, with a solid fill, the button works correctly.

Takeaway lesson: if you want to hide your shape but still have it respond to user interaction, use a transparent brush, not a null brush.

我的奇迹 2024-08-18 01:05:22

我对自定义模板按钮也遇到了类似的问题:

     <my:UniButton Command="{Binding MyCommand}"/>

在添加relativesource之前绑定不起作用:

     <my:UniButton Command="{Binding MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=my:CustomPanel}}"/>

其中custompanel是我的按钮所在的控件。

另外,我在同一面板上有一个简单的按钮,但即使没有relativesource,它也能正常工作。

I had a similar problem with a custom templated button:

     <my:UniButton Command="{Binding MyCommand}"/>

The binding didn't work until adding a RelativeSource:

     <my:UniButton Command="{Binding MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=my:CustomPanel}}"/>

where CustomPanel is a control where my button lies.

Withal I had a simple button on the same panel, but it worked fine even without RelativeSource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文