具有自定义 ControlTemplate 的按钮有时会错过点击

发布于 2024-12-27 10:07:59 字数 1113 浏览 1 评论 0原文

我的带有自定义 ControlTemplateButton 不会响应每次点击。我不知道为什么,因为点击是否有效是完全随机的。

控制模板:

<ControlTemplate x:Key="KreisPlus">
    <Path Data="M74,37.5 C74,57.658393 57.658393,74 37.5,74 C17.341607,74 1,57.658393 1,37.5 C1,17.341607
            17.341607,1 37.5,1 C57.658393,1 74,17.341607 74,37.5 z M32.113861,13.5 L43.386139,13.5 43.386139,
            32.613861 62.5,32.613861 62.5,43.886139 43.386139,43.886139 43.386139,63 32.113861,63 32.113861,
            43.886139 13,43.886139 13,32.613861 32.113861,32.613861 z"
            Height="20" Stretch="Fill" Stroke="#FFD7D7D7" Width="20">
        <Path.Fill>
            <RadialGradientBrush>
                <GradientStop Color="#FF1F71D5" />
                <GradientStop Color="White" Offset="0.94" />
                <GradientStop Color="#FF6499E0" Offset="0.78" />
            </RadialGradientBrush>
        </Path.Fill>
    </Path>
</ControlTemplate>

<Button Click="Button_Click" Template="{StaticResource ResourceKey=KreisPlus}" />

My Button with a custom ControlTemplate does not respond to every click. I have no idea why, since it is totally random if the click works or not.

ControlTemplate:

<ControlTemplate x:Key="KreisPlus">
    <Path Data="M74,37.5 C74,57.658393 57.658393,74 37.5,74 C17.341607,74 1,57.658393 1,37.5 C1,17.341607
            17.341607,1 37.5,1 C57.658393,1 74,17.341607 74,37.5 z M32.113861,13.5 L43.386139,13.5 43.386139,
            32.613861 62.5,32.613861 62.5,43.886139 43.386139,43.886139 43.386139,63 32.113861,63 32.113861,
            43.886139 13,43.886139 13,32.613861 32.113861,32.613861 z"
            Height="20" Stretch="Fill" Stroke="#FFD7D7D7" Width="20">
        <Path.Fill>
            <RadialGradientBrush>
                <GradientStop Color="#FF1F71D5" />
                <GradientStop Color="White" Offset="0.94" />
                <GradientStop Color="#FF6499E0" Offset="0.78" />
            </RadialGradientBrush>
        </Path.Fill>
    </Path>
</ControlTemplate>

<Button Click="Button_Click" Template="{StaticResource ResourceKey=KreisPlus}" />

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

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

发布评论

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

评论(1

绝不服输 2025-01-03 10:07:59

只需尝试此代码示例,看看这是您所期望的吗?

        <ControlTemplate x:Key="KreisPlus" TargetType="{x:Type Button}">

        <Grid HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Background="Transparent" >
        <Path Data="M74,37.5 C74,57.658393 57.658393,74 37.5,74 C17.341607,74 1,57.658393 1,37.5 C1,17.341607 17.341607,1 37.5,1 C57.658393,1 74,17.341607 74,37.5 z M32.113861,13.5 L43.386139,13.5 43.386139,32.613861 62.5,32.613861 62.5,43.886139 43.386139,43.886139 43.386139,63 32.113861,63 32.113861,43.886139 13,43.886139 13,32.613861 32.113861,32.613861 z"
              Height="20"
              Stretch="Fill"
              Stroke="#FFD7D7D7"

              Width="20">
            <Path.Fill>
                <RadialGradientBrush>
                    <GradientStop Color="#FF1F71D5" />
                    <GradientStop Color="White"
                                  Offset="0.94" />
                    <GradientStop Color="#FF6499E0"
                                  Offset="0.78" />
                </RadialGradientBrush>
            </Path.Fill>
        </Path>
        </Grid>
    </ControlTemplate>

现在路径位于网格内部,因此它可以响应单击。这将占据整个区域,并且仅占据中心的路径。因此,如果您在任何地方单击按钮,即使是外部路径也会执行单击。如果您不希望这样并且希望路径响应单击,只需将水平和垂直对齐更改为中心

HorizontalAlignment="Center"
VerticalAlignment="Center"

just try this code sample and see is that what you expect?

        <ControlTemplate x:Key="KreisPlus" TargetType="{x:Type Button}">

        <Grid HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Background="Transparent" >
        <Path Data="M74,37.5 C74,57.658393 57.658393,74 37.5,74 C17.341607,74 1,57.658393 1,37.5 C1,17.341607 17.341607,1 37.5,1 C57.658393,1 74,17.341607 74,37.5 z M32.113861,13.5 L43.386139,13.5 43.386139,32.613861 62.5,32.613861 62.5,43.886139 43.386139,43.886139 43.386139,63 32.113861,63 32.113861,43.886139 13,43.886139 13,32.613861 32.113861,32.613861 z"
              Height="20"
              Stretch="Fill"
              Stroke="#FFD7D7D7"

              Width="20">
            <Path.Fill>
                <RadialGradientBrush>
                    <GradientStop Color="#FF1F71D5" />
                    <GradientStop Color="White"
                                  Offset="0.94" />
                    <GradientStop Color="#FF6499E0"
                                  Offset="0.78" />
                </RadialGradientBrush>
            </Path.Fill>
        </Path>
        </Grid>
    </ControlTemplate>

Now the path is inside the Grid and so it can respond to the click. This will take the entire area and only the path in the center. So if you click on the button anywhere even outside path will execute the click. If you don't want this and you want the path should respond to the click just change horizontal and vertical alignment as center

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