1、添加新项

2、在构造函数中加入

    public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Grid grid = null;
String xamlFile = @"C:\Users\zgj\source\repos\WindowsFormsApp1\test.xaml";
using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
{
grid = (Grid)XamlReader.Load(fs);
}
grid1.Children.Add(grid);
}
}

3、test.xaml内容

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="test_panel">
<Button x:Name="but1" Height="25" Width="60">but1</Button>
<Button x:Name="but2" Height="25" Width="60">but2</Button>
</StackPanel>
</Grid>

4、将usercontrol拖到winform窗体上即可显示

5、如果test.xaml的根节点为window,需要分离,注意加黑部分。

 /// <summary>
/// UserControl1.xaml 的交互逻辑
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Window window = null;
String xamlFile = @"C:\Users\zgj\source\repos\WindowsFormsApp1\test.xaml";
using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
{
window = (Window)XamlReader.Load(fs);
}
Grid grid = (Grid)window.Content;
DependencyObject parent = grid.Parent;
if (parent != null)
{
parent.SetValue(ContentPresenter.ContentProperty, null
);
}

grid1.Children.Add(grid);
}
}
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="test" Height="300" Width="300">
<Grid>
<StackPanel x:Name="test_panel">
<Button x:Name="but1" Height="25" Width="60">but1</Button>
<Button x:Name="but2" Height="25" Width="60">but2</Button>
</StackPanel>
</Grid>
</Window>

源程序在QQ群:616945527,winform加载WPF例子。

Winform中使用WPF控件并动态读取Xaml的更多相关文章

  1. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...

  2. 在Winform窗体中使用WPF控件(附源码)

    原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件.原有是我在百度上搜索相关信息无果,遂干脆动手自己实现. W ...

  3. WPF中嵌入WinForm中的webbrowser控件

    原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...

  4. 在WinForm应用程序中嵌入WPF控件

    我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...

  5. Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. winfrom窗体中嵌套WPF控件

    前言 本文主要介绍如何在winfrom窗体中嵌套WPF控件, 一来是自己记录一下,而来希望能对有需要的朋友提供实现思路. 如有错误请指出...下面进入正题... -1.前期准备 准备一个建立好的win ...

  7. winform中的dateTimePicker控件设置默认值为空

    winform中的dateTimePicker控件设置默认值为空   第一步:设置Format的属性值为“Custom” 第二步:设置CustomFormat的属性值为空,需要按一个空格键

  8. C#在WinForm中重写ProgressBar控件(带%的显示)

    废话少说,直接上码: namespace csPublish { [ToolboxItem(true)] class textProgressBar : System.Windows.Forms.Pr ...

  9. Sparrow.Chart.Wpf控件的动态调用

    最近需要在Wpf程序中显示曲线,感觉Sparrow.Chart.Wpf控件不错(http://sparrowtoolkit.codeplex.com/),完全开源的一个控件支持,可以通过nuget下载 ...

随机推荐

  1. Angular $scope和$rootScope

    <!DOCTYPE html><html ng-app='myModule'><head lang="en"> <meta charset ...

  2. Jquery ajax $getScript()和$getJSON和JSONP

  3. Alpha、伪Beta 发布个人感想与体会

    1.Alpha版本 在Alpha版本发布时,我在Fantacy组,那时的体会我已在前面写过,现在回想起来,我觉得自己的决定似乎做的并不是很糟糕,因为来到新的团队里,我学到了很多东西,认识了很多技术很好 ...

  4. Security Testing Test Scenarios

    1 check for sql injection attacks2 secure pages should use https protocol3 page crash should not rev ...

  5. xslt格式化日期的方法

    数据:<PK_SEND_DATE>2007-9-28 0:00:00</PK_SEND_DATE> 通过截取:<xsl:value-of select="sub ...

  6. 【BZOJ4712】洪水(动态dp)

    [BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...

  7. 洛谷 P2466 Sue的小球 解题报告

    P2466 [SDOI2008]Sue的小球 题目描述 Sue和Sandy最近迷上了一个电脑游戏,这个游戏的故事发在美丽神秘并且充满刺激的大海上,Sue有一支轻便小巧的小船.然而,Sue的目标并不是当 ...

  8. SSM框架中的前后端分离

    认识前后端分离 在传统的web应用开发中,大多数的程序员会将浏览器作为前后端的分界线.将浏览器中为用户进行页面展示的部分称之为前端,而将运行在服务器,为前端提供业务逻辑和数据准备的所有代码统称为后端. ...

  9. Redis我想入门——启动

    nosql数据库出现到现在很多年了.笔者一直从事C/S模式的上开发.所以相对而言笔者只是听过却从来不知道他是什么东西.时代在变化,当年所有业务都放在一个War包的时代已经不在了.微服务已经成为了世界主 ...

  10. A1008. Elevator

    The highest building in our city has only one elevator. A request list is made up with N positive nu ...