定义新的 ControlTemplate 保留原始事件

发布于 2024-12-10 06:47:10 字数 694 浏览 5 评论 0原文

我想使用 ExtendedWPFToolkits 的 ColorPicker,但使用自定义 ButtonStyle。 我可以创建一个覆盖项目的模板属性的新外观,但原始模板单击事件丢失。

我想保留它,但是怎么保留呢?

<Controls:ColorPicker >
    <Controls:ColorPicker.ButtonStyle>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Button Content="ColorPicker"></Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Controls:ColorPicker.ButtonStyle>
</Controls:ColorPicker>

I want to use the ExtendedWPFToolkits's ColorPicker but with a custom ButtonStyle.
I can create a new look overriding the Template property of the item but the original templates click event is missing.

I want to keep it, but how?

<Controls:ColorPicker >
    <Controls:ColorPicker.ButtonStyle>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Button Content="ColorPicker"></Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Controls:ColorPicker.ButtonStyle>
</Controls:ColorPicker>

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

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

发布评论

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

评论(1

东京女 2024-12-17 06:47:10

您拥有的内容无效。您将一个 Button 放入 ToggleButton 的 ControlTemplate 中,因此基本上是一个按钮中的按钮。

您需要执行以下操作:

<Controls:ColorPicker >
    <Controls:ColorPicker.ButtonStyle>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Background="Transparent">
                            <TextBlock Text="ColorPicker" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Controls:ColorPicker.ButtonStyle>
</Controls:ColorPicker>

我添加了一个透明的边框,以便按钮能够接收文本未覆盖的区域的鼠标事件。

What you have is not valid. You are putting a Button in the ControlTemplate of a ToggleButton, so basically a button in a button.

You'd need to do something like:

<Controls:ColorPicker >
    <Controls:ColorPicker.ButtonStyle>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Background="Transparent">
                            <TextBlock Text="ColorPicker" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Controls:ColorPicker.ButtonStyle>
</Controls:ColorPicker>

I added a transparent Border so the button will be able to receive mouse events for areas not covered by the text.

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