从GDI / WinForms切换到WPF以用于自定义UI元素 [英] Switching from GDI/WinForms to WPF for custom UI elements

查看:85
本文介绍了从GDI / WinForms切换到WPF以用于自定义UI元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用WinForms 6年以上之后,我正在过渡到使用WPF。以前,使用 On_Paint 并使用图形和画笔等进行绘制,我创建了如下的自定义UI元素:

I'm transitioning to using WPF after using WinForms for 6+ years. Previously, using On_Paint, and drawing with Graphics and Brushes and the like, I've created custom UI elements like the below:

我想复制此外观使用WPF,但我不确定从哪里开始或如何做。谢谢您的帮助。

I'd like to duplicate this look using WPF but I'm not sure where to begin or how to do it. Thanks for any help.

推荐答案

基本答案是,您需要覆盖 ControlTemplate 为您正在设置样式的按钮(在资源中重复使用此代码!)。

The basic answer is that you need to override the ControlTemplate for the button you are styling (do this in a resource for code reuse!).

基本格式为:

<Window.Resources>
    <Style TargetType="Button" Key="MyAwesomeButtonStyle">
       ... Bunch of stuff including control template
    </Style>
</Window.Resources>
<Grid>
   <Button Style="{StaticResource MyAwesomeButtonStyle}"/>
</Grid>

要了解现有控制模板的外观,请查看 MSDN 。对于特定的按钮,请查看按钮样式和模板(您可以通过向下滚动一点并单击按钮样式和模板链接从第一个链接到达那里)。

To get a starting point for what the existing control template looks at, look at MSDN. For a button specifically, look at Button Styles and Templates (you can get there from the first link by scrolling down a little and clicking the "Button Styles and Templates" link).

要匹配您的图片,您可能需要用简单的<$ c $替换环绕式 Border 元素c> Grid 并添加 Path 以获得平行四边形形状。 RadialGradientBrush 可用于获取颜色效果的路径背景。

To match your picture, you will likely need to replace the wrapping Border element with as simple Grid and add a Path to get the parallelogram shape. RadialGradientBrush can be used for the background of the path to get the color effect.

没有一个更狭窄的问题,很难具体说明您需要什么(而无需花费时间来找出模板),但是我的建议是将现有的控制模板放入程序中,并将其用作替代您的按钮。然后修改片段以了解它们的作用/影响。最后,修改正确的片段以使其看起来像您想要的。

Without a more narrow question, its hard to say specifically what you need (without taking the time to just figure out the template), but my advice would be to put the existing control template into your program and use it as an override for one of your buttons. Then modify pieces to learn what they do/affect. Finally, modify the correct pieces to look like you want.

就学习如何修改/编写这些内容而言,试错可能是最好的选择。

Basically, trial and error is probably your best bet as far as learning how to modify/write these are concerned.

这篇关于从GDI / WinForms切换到WPF以用于自定义UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆