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. ubuntu 下搭建redis和php的redis的拓展

    系统环境: 腾讯云服务器, ubuntu16.0.4.4 ,php7.0   一.安装redis服务 sudo apt-get install redis-server 安装好的redis目录在 /e ...

  2. Laravel Eloquent ORM 时如何查询表中指定的字段

    导读:在使用Laravel ORM的Model方法find, get, first方法获取数据对象时返回的数据对象的attributes属性数组里会包含数据表中所有的字段对应...原文地址:http: ...

  3. 选择 Delphi 2007 ( CodeGear Delphi 2007 for Win32 Version 11.0.2837.9583 ) 的理由

    选择 Delphi 2007 ( CodeGear Delphi 2007 for Win32 Version 11.0.2837.9583 ) 的理由 我不喜欢用InstallRite的全自动安装包 ...

  4. JavaScript——数组

    数组是JavaScript中的常用类型,本文详述了数组的基本知识以及一些常用的数组方法,并对每种方法进行了详细解释 数组定义 用字面量直接定义 1 var arr=[0,0,0]; //注意,是方括号 ...

  5. map()实现zip()功能

    c = (map(lambda x,y:(x,y),[1,2,3],["abd","def","ghi"]))print(list(c)) ...

  6. HDU - 1260 (Tickets)

    题意:  买票    一个人要么自己买  要么和前面的人一起买   这两种情况分别有一个买票所花费的时间  求总的花费时间最小 解析: dp[i] 表示前i个人买票总的花费时间 v[i]表示第i个人买 ...

  7. LOJ #2721. 「NOI2018」屠龙勇士(set + exgcd)

    题意 LOJ #2721. 「NOI2018」屠龙勇士 题解 首先假设每条龙都可以打死,每次拿到的剑攻击力为 \(ATK\) . 这个需要支持每次插入一个数,查找比一个 \(\le\) 数最大的数(或 ...

  8. 用powershell 批量卸载 windows 更新

    $KBID = "KB958488" $KBID1 = "KB976902" cls function Remove-Update { $HotFixes = ...

  9. install kubernetes cluster k8s集群安装

    一,安装docker-ce 17.031,下载rpm包 Wget -P /tmp https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/ ...

  10. luogu3320 寻宝游戏 (dfs序+倍增lca+set)

    一定是从随便某个点开始,然后按着dfs序的顺序跑一圈是最好的 所以说,新加一个点x,就减少了dis(pre,next),增加了dis(pre,x),dis(x,nxt) 删掉一个点同理 这个可以用se ...