创建一个按钮模板编程 [英] Create a Button template programatically

查看:117
本文介绍了创建一个按钮模板编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我很抱歉,我的英语我希望你明白我的问题(更正接受,接下来的文章会更正确的)。

First, I'm sorry about my English I hope you understand my problem (Corrections accepted and the next post will be more correct).

OK,这是我的方案:对于Windows Phone 7的,我不能使用XAML模板开发。我需要在运行时创建的C#代码的模板。我有自定义的唯一元素是无效状态前景色和背景色,当用户握住按钮和一个圆角的边框。

OK, this is my scenario: Developing for Windows Phone 7. I can't use XAML templates. I need to create a template at run time with C# code. The only elements that I have to customize are the background and foreground colors in inactive state and when the user holds the button, and a rounded corner for the border.

设置在 Button.Style 属性只能默认状态(未激活),我不能创建一个拐角半径:

Setting up the Button.Style property works only for the default state (inactive) and I can't create a corner radius:

Style buttonStyle = new Style(typeof(Button); 

buttonStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Colors.Blue)); 
buttonStyle.Setters.Add(new Setter(Button.ForegroundProperty, new SolidColorBrush(Colors.White))); 

Button myBytton = new Button();
myButton.Style = buttonStyle;

下一步是建立对于Button.Template属性CustomTemplate,但总是引发一个例外:

The next step was to create a CustomTemplate for the Button.Template property, but always raises an Exception:

CustomTemplate myTemplate = new CustomTemplate();
myTemplate.TargetType = typeof(Button); // Exception raises here: InvalidOperationException wihtout more info

(..继续代码...)

(..Continues code ...)

Button myBytton = new Button();
myButton.Template = myTemplate;



我浏览过用XAML的CutomTemplate的例子。我不知道如何设置样式不同的按钮状态和圆角半径。

All examples I've viewed used XAML for the CutomTemplate. I don't know how to setup styles for different button states and for the border radius.

如果有人能帮助我......感谢您的阅读,

If anybody can help me... Thanks for reading,

推荐答案

此问题已经在MSDN论坛解决:

This issue has been resolved in the MSDN forums:

HTTP :?//social.msdn.microsoft.com/Forums/wpapps/en-us/c303bc14-2964-4655-9b04-be6a8fc39b19/customize-button-template-wp7论坛= wpdevelop

: - )

这篇关于创建一个按钮模板编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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