样式中的用户控件属性
我有一个公开这样的公共属性的用户控件:
public Double ButtonImageHeight
{
get { return imgButtonImage.Height; }
set { imgButtonImage.Height = value; }
}
当我使用该控件时,我希望能够通过这样的样式设置该属性:
<Style x:Key="MyButtonStyle" TargetType="my:CustomButtonUserControl" >
<Setter Property="ButtonImageHeight" Value="100" />
</Style>
我做错了什么?
谢谢
I have a usercontrol that expose a public property like this :
public Double ButtonImageHeight
{
get { return imgButtonImage.Height; }
set { imgButtonImage.Height = value; }
}
when I use that control, I want to be able to set that property throught a Style like that :
<Style x:Key="MyButtonStyle" TargetType="my:CustomButtonUserControl" >
<Setter Property="ButtonImageHeight" Value="100" />
</Style>
What am I doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
谢谢马特,我自己刚刚找到它,但你是绝对正确的...这是我使用的确切代码,以防它可以帮助其他人(我找到的所有示例都在 WPF 上,silverlight 只是略有不同):
thanks Matt, I just found it myself but you were absolutely right... here's the exact code I used in case it can help someone else (all the examples I found were on WPF, silverlight is just slightly different) :
该属性需要是依赖属性才能支持样式。
The property needs to be a dependency property in order to support styles.
您可以通过传递 imgButtonImage 的 Style 使其更加通用和美观,这样您就可以设置多个属性。因此,在您的用户控件中添加依赖属性,但将其设置为样式:
请注意,我如何在 PropertyChanged 函数中将控件的样式设置为新样式。
然后,当我托管 UserControl 时,我可以传递 style:
也可以在 VS 设计模式下工作! (这是一个奇迹)
You can make it even more generic and nice by passing through a Style for your imgButtonImage, that way you can set multiple properties. So within your user control add the dependency property, but make it a Style:
Notice how within the PropertyChanged function I set the style of the control to the new style.
Then when I host the UserControl I can pass through the style:
works in VS design mode too! (It's a miracle)