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. [工作相关] 一个虚拟机上面的SAP4HANA的简单使用维护

    1.公司组织竞品分析, 选择了SAP的 SAP4HANA作为竞品 这边协助同事搭建了SAP4HANA的测试环境: 备注 这个环境 应该是同事通过一些渠道获取到的. 里面是基于这个虚拟机进行的说明:: ...

  2. webpack & bundle analyzer

    webpack & bundle analyzer webpack bundle analyzer https://github.com/th0r/webpack-bundle-analyze ...

  3. fix

    rounds the elements of A toward zero, resulting in an array of integers. For complex A, the imaginar ...

  4. Java 8新特性之 Base64(八恶人-7)

    "General" 我是个将军 “ You, sir a hyena. I hava no wish to speak to you.”  “你就是一个土狗,你不配跟我说话” 一. ...

  5. 51Nod 2006 飞行员配对(二分图最大匹配)

    第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...

  6. 自学Linux Shell6.2-用户自定义环境变量

    点击返回 自学Linux命令行与Shell脚本之路 6.2-用户自定义环境变量 1.设置局部用户定义变量 一旦启动bash shell(或者执行一个shell脚本),你就能创建这个shell进程可见的 ...

  7. 【BZOJ2302】[HAOI2011]Problem C(动态规划)

    [BZOJ2302][HAOI2011]Problem C(动态规划) 题面 BZOJ 洛谷 题解 首先如果\(m=0\)即没有特殊限制的话,那么就和这道题目基本上是一样的. 然而这题也有属于这题的性 ...

  8. luogu1072 [NOIp2009]Hankson的趣味题 (数学+STL::set)

    一个JSB做法 由$\frac{x*b0}{gcd(x,b0)}=b1$,可得$\frac{x}{gcd(x,b0)}=\frac{b1}{b0}$ 设$b2=\frac{b1}{b0}$ 所以对$b ...

  9. DownloadProvider 源码详细分析

    DownloadProvider 简介 DownloadProvider 是Android提供的DownloadManager的增强版,亮点是支持断点下载,提供了“开始下载”,“暂停下载”,“重新下载 ...

  10. P1558 色板游戏 线段树+二进制状压

    好,这个想法是我想拿去做HH的项链的.但是那个颜色有十万种...直接爆. 做这个倒是so easy 被两个地方坑了.1,a,b可能大小相反. 2,ask之前要down一波,我没down就挂了..... ...