Switching from GDI/WinForms to WPF for custom UI elements

☆樱花仙子☆ 提交于 2019-12-11 09:40:39

问题


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:

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.


回答1:


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!).

The basic format would be:

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

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).

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.




回答2:


When delving into WPF, it's best to forget everything you know about Win Forms and just pretend you're learning how to develop UIs for the first time. Pretty much everything is different, so expect a good year or so of study and work before it really starts to gel.

You can do a lot with Control Templates, though your particular controls are complex enough in appearance that hard-coding the Paths for the borders may be a bit awkward. You can get something close to the old GDI way of doing things using DrawingVisual, which lets you define vector graphics in code.

Note that you even if you did use a DrawingVisual to get the particular look you're after, you'd still probably want to use it inside a ControlTemplate for Button. This way you can take advantage of all the code that's already been written for Button and anybody using your template doesn't have to change out their Buttons for custom controls; all they have to do is apply your template to change how it looks.

Again, this is a deep and complex topic, so expect to do a lot of reading/tinkering before you have all of the concepts down. Best of luck and happy coding; WPF is well worth the pain of entry.



来源:https://stackoverflow.com/questions/24664098/switching-from-gdi-winforms-to-wpf-for-custom-ui-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!