您的位置:首页 > Web前端 > CSS

在win-form 应用程序中使用WPF控件和样式

2011-12-11 20:26 363 查看
在win-form 应用程序中使用WPF控件和样式

1. 新建一个win-form的应用程序

2. 将含有资源文件WPFResourceDemo.xaml的工程添加为该工程的引用, 如何构建WPF的资源工程请参考:

http://blog.csdn.net/farawayplace613/article/details/7061509

3. 在win-form的窗口上拖入一个 element host ,并命名为 elementHost1



4.  在win-form的后台代码中Form1 () 或Load事件的方法中加入如下代码: 

// 初始化一个 WPF中的Button控件
System.Windows.Controls.Button button = new System.Windows.Controls.Button();
ResourceDictionary dict = new ResourceDictionary();

// 注意:此处不可以移动到  dict.Source = ...之后
if (System.Windows.Application.Current == null)
{
System.Windows.Application app = new System.Windows.Application();
}

// 导入WPF的样式
dict.Source = new Uri("pack://application:,,,/WPFResourceDemo;component/WPFResourceDemo.xaml");

// 将WPF样式加入系统默认的资源池
System.Windows.Application.Current.Resources.MergedDictionaries.Add(dict);

// 应用 WPF样式
button.Style = (Style)System.Windows.Application.Current.Resources["CircleButtonStyle"];
elementHost1.Child = button;

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wpf button null