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. MongoDb在windows10下的安装、创建用户和数据库

    1.mongodb下载地址https://www.mongodb.com/download-center#community 2.安装    3.在D:\MongoDB目录下创建db和log两个文件夹 ...

  2. 微信小程序不能超过十个并发的解决办法

    一般是封装一个请求队列,将请求对象存入队列,在complete写队列的出队操作.

  3. Linux基础学习(5)--文本编辑器Vim

    第五章——文本编辑器Vim 一. Vim常用操作 1.Vim简介:              Vim是一个功能强大的全屏幕文本编辑器,是Linux/UNIX上最常用的文本编辑器,它的作用是建立.编辑. ...

  4. delphi property read writer 如何使用

    type TMyClass = class(TObject) private FMyName: string; FMyAge: Integer; procedure SetAge(age: Integ ...

  5. C/S架构引用Lodop 如何在C#调用web打印控件Lodop

    lodop是web打印控件,引用安装目录下的ocx文件,可以在c/s架构中使用. 该文件所在路径:C:\Program Files (x86)\MountTaiSoftware\Lodop 有32位和 ...

  6. tomcat 查看和修改内存

    为了解决tomcat在大进行大并发请求时,出现内存溢出的问题,请修改tomcat的内存大小,其中分为以下两种方式: 一.使用 catalina.bat 等命令行方式运行的 tomcat 查看系统最大支 ...

  7. Django-website 程序案例系列-16 modle.form(表单验证)

    案例程序: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. Codeforces976E Well played! 【贪心】

    题目分析: 由于乘二的收获很大,所以我们可以证明乘的数一定是同一个,接着排序后依次选取,判断一下即可. 题目代码: #include<bits/stdc++.h> using namesp ...

  9. hdu5306 Gorgeous Sequence

    hdu5306 Gorgeous Sequence 题目大意 ​ 给你一个序列,维护区间和,区间chkmin和区间最大值 数据范围 数据组数T,序列长度n,操作次数m $T = 100,\sum n ...

  10. BAI度 内部资料!Python_Threads多线程

    基本介绍 runable运行sleeping等待dead销毁(run方法执行完成或执行时抛出异常) 类继承threading.Thread线程的状态 函数介绍 在__init__里调用threadin ...