在WPF中创建自定义形状的按钮 [英] Create custom shaped buttons in WPF

查看:72
本文介绍了在WPF中创建自定义形状的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要创建一个自定义形状的按钮,看起来像一个向前的箭头.三个或四个这样的按钮应彼此相邻放置,以使第一个按钮的尖端恰好位于下一个按钮的尾部附近


我已经使用.xaml&中的多边形为按钮制作了样式将此样式分配给按钮.现在,如果我将此按钮添加到表单中,则可以按所需样式看到此按钮.
但是,按钮仍然具有矩形形状,在边框和边框中不可见.其余区域是透明的.因此,无论我创建的样式如何,按钮都在表单上占据一个矩形区域.

因此,我无法将前向形状的丁字扣彼此保持尽可能近的距离.在这里,要使一个前进按钮的尖端靠近下一个按钮的尾部,按钮的矩形区域必须重叠,这是不正确的.


有什么方法可以删除按钮&的矩形区域吗?使其完全占据样式指定的所需形状.


我的代码的.xaml文件在下面给出

Hello,

I need to create a custom shaped button which looks like a forward arrow. Three or four of such buttons are to be placed next to each other in such a way that the tip of the first button is just near the tail of the next button


I have made a style for the button using polygon in .xaml & assigned this style to the button. Now, if i add this button to a form, i can see this button in the desired style.
But, still the button has the rectangular shape, which is not visible as the borders & remaining area are transparent. So, the button occupies a rectangular area on the form irrespective of the style which i created.

Because of this, i cannot keep the forward shaped butons next to each other, as close as desired. Here, for the tip of one forward button to come near the tail of the next button, the rectangular area of the buttons must overlap, which is not correct.


Is there any way to remove the rectangular area of the button & make it exactly to occupy the desired shape as specified by the style.


The .xaml file for my code is given below

<window x:class="Styles.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="525">
    <window.resources>
<Style x:Key="Poly1" TargetType="{x:Type Button}">
        	<setter property="Template">
        		<setter.value>
        			<controltemplate targettype="{x:Type Button}">
        				<grid>
                            <polygon fill="{TemplateBinding Background}" points="10,10 80,10 120,45 80,75 10,75 45,45" stroke="{TemplateBinding BorderBrush}" strokethickness="2" />
        					<contentpresenter horizontalalignment="{TemplateBinding HorizontalContentAlignment}" recognizesaccesskey="True" snapstodevicepixels="{TemplateBinding SnapsToDevicePixels}" verticalalignment="{TemplateBinding VerticalContentAlignment}" />
        				</grid>
        				<controltemplate.triggers>
        					<trigger property="IsFocused" value="True" />
        					<trigger property="IsDefaulted" value="True" />
        					<trigger property="IsMouseOver" value="True" />
        					<trigger property="IsPressed" value="True" />
        					<trigger property="IsEnabled" value="False" />
        				</controltemplate.triggers>
        			</controltemplate>
        		</setter.value>
        	</setter>
        </Style>
</window.resources>
<wrappanel>
        <Button Style="{DynamicResource Poly1}"></Button>
        <Button Style="{DynamicResource Poly1}"></Button>
 </wrappanel>
</window>

推荐答案



不,我们世界上的所有事物都生活在矩形空间中……这是将任意事物组合在一起的最佳方法(如果有人在这样的按钮之间放置一条垂直线,您会怎么想?)

您的特殊情况需要特殊的解决方案-实际上几乎是微不足道的:使用负边距(至少:右边距)...并且不要在矩形空间的中间任何地方"开始多边形(这意味着:从0,0开始而不是10,10).

干杯
于尔根(Jürgen)
Hi,

no, everything in our world lives in rectangular spaces ... which is best for combining arbitrary things (what would you excpect if someone places a vertical line between such butons).

Your special case needs a special solution - which in fact is almost trivial: use negative margins (at least: right margin) ... and don''t start your polygon ''anywhere in the middle'' of the rectangular space (meaning: start at 0,0 not 10,10).

Cheers
Jürgen


这篇关于在WPF中创建自定义形状的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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