简体   繁体   English

Silverlight:如何使我的自定义控件像按钮一样

[英]Silverlight: How to make my custom control act like a button

I have a custom control and I would like it to act like a button ie when you hover over it changes a little so it seems "clickable" to the user 我有一个自定义控件,我希望它像按钮一样起作用,即,当您将鼠标悬停在它上面时,它会有所变化,因此对用户来说似乎是“可点击的”

I actually acheived this using the MouseEnter and MouseLeave events and changing the gradient but... 我实际上使用MouseEnter和MouseLeave事件并更改渐变来实现这一点,但是...

is there a way to apply a style to the user control and say something like TargetType="button" so that it "acts" like a button automatically? 有没有一种方法可以将样式应用于用户控件并说出类似TargetType =“ button”之类的内容,以使其自动像按钮一样“起作用”?

I feel the way i'm doing it is not the best way 我觉得我做事的方式不是最好的方法

As sniper says, you can set a Controltemplate for each state. 正如狙击手所说,您可以为每个状态设置一个Controltemplate。

Alternatively, you can completely replace a control's visual tree with anything you want - while still keeping the control behavior intact. 或者, you can completely replace a control's visual tree with anything you want - while still keeping the control behavior intact. Check out this post by ScottGu on the topic 查看ScottGu的有关主题的帖子

In Expression Blend 3, you can edit the different states (Normal, Hover, pressed, selected etc), of any control how you need it. 在Expression Blend 3中,您可以根据需要编辑任何控件的不同状态(“正常”,“悬停”,“按下”,“选定”等)。 just select your control and click Edit copy template 只需选择您的控件,然后单击“ Edit copy template

Add border object and sets its visibility on mousehover and leave event on the control (This will look like a flat\\popup button). 添加边框对象,并在鼠标悬停时设置其可见性,并在控件上留下事件(这看起来像一个flat \\ popup按钮)。 Additionally set the control's cursor to hand. 另外,将控件的光标设置为hand。

您可以从ButtonBase派生控件,就像Button,HyperlinkBut​​ton,Checkbox等一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM