转载自 http://www.cnblogs.com/shuang121/archive/2013/01/09/2853591.html

我们来新建一个用户控件UserControl1.xaml

<UserControl x:Class="WpfApplicationDemo.Control.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Border BorderThickness="3" CornerRadius ="5"
               Background="#FFFFCC" BorderBrush="#FF6633">
            <StackPanel Orientation="Vertical" Margin="5"                      HorizontalAlignment="Center">
                <Image Name="goodsImage" Height="80" Width="80" Margin="5"></Image>
                <TextBlock Name="goodsPrice" Margin="5"></TextBlock>
                <TextBlock Name="goodsQty" Margin="5"></TextBlock>
                <Image Name="goodsBuy" Source="/images/fbxq_an.gif"                      Height="25" Width="25" Cursor="Hand" Margin="5">
                    <Image.ToolTip>Add Quantity</Image.ToolTip>
                </Image>
            </StackPanel>
        </Border>
    </Grid>
 
</UserControl>

在新建一个Window窗体,把用户控件添加到Window窗体中

方法如下:

方法一:在xmal中添加

首先、要引用用户控件的命名控件 xmlns:my="clr-namespace:WpfApplicationDemo.Control"

然后、把用户控件添加到窗体中

<my:UserControl1 HorizontalAlignment="Left" Margin="38,46,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="183" Width="215" />

代码如下:

<Window x:Class="WpfApplicationDemo.UserControlDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplicationDemo.Control"
Title="UserControlDemo" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="下面是用户控件" VerticalAlignment="Top" />
<StackPanel Height="175" HorizontalAlignment="Left" Margin="20,57,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="246" />
<my:UserControl1 HorizontalAlignment="Left" Margin="38,46,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="183" Width="406" /> </Grid>
</Window>

方法二:在cs代码中添加

比如我们把用户控件放到容器中

<StackPanel Height="175" HorizontalAlignment="Left" Margin="20,57,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="246" ></StackPanel>

然后在后台代码中,实例化用户控件,添加到容器中即可

    public partial class UserControlDemo : Window
{
public UserControlDemo()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
UserControl1 demo = new UserControl1();
this.stackPanel1.Children.Add(demo);
}
}

wpf的UserControl用户控件怎么添加到Window窗体中的更多相关文章

  1. WPF 精修篇 用户控件

    原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...

  2. WPF之路——用户控件对比自定义控件UserControl VS CustomControl)

    将多个现有的控件组合成一个可重用的“组”. 由一个XAML文件和一个后台代码文件. 不能使用样式和模板. 继承自UserControl类. 自定义控件(扩展) 在现有的控件上进行扩展,增加一些新的属性 ...

  3. WPF学习- AllowDrop 用户控件启用拖放功能

    知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) ...

  4. [WPF 学习] 3.用户控件库使用资源字典的困惑

    项目需要(或者前后端分离的需要),前端我使用了用户控件库,由后端用代码加载和控制. 然而用户控件库没法指定资源字典,于是在用户控件的xaml文件里面手工添加了资源字典 <UserControl. ...

  5. WPF窗口和用户控件事件相互触发

    问题1: WPF项目里有一个窗口和一个用户控件,窗口和用户控件里都有一个Button,点击窗口里的Button如何触发用户控件里Button的Click事件 解答: //窗口代码 public par ...

  6. WPF获取当前用户控件的父级窗体

    方式一.通过当前控件名获取父级窗体 Window targetWindow = Window.GetWindow(button); 方式二.通过当前控件获取父级窗体 Window parentWind ...

  7. Winform 后台将指定的控件集合添加到制定容器中

    /// <summary> /// 把按钮按照行数分割排列 /// </summary> /// <param name="ControlArry"& ...

  8. 【demo练习四】:WPF用户控件案例

    首先,新建vs中“用户控件(WPF)”,右键项目名 =>"添加"按钮 => 选择“新建项”. 然后选择“用户控件(WPF)” => 起名字 => 点击“添加 ...

  9. 用户控件(UserControl)

    简介 "用户控件"继承自UserControl,而UserControl继承自ContentControl,也就是内容控件UserControl和Window是一个层次上的,都有x ...

随机推荐

  1. MySQL隐式转化整理

    MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...

  2. Mui.ajax请求服务器正确返回json数据格式

    ajax: mui.ajax('http://server-name/login.php',{ data:{ username:'username', password:'password' }, d ...

  3. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(二)

    Log4j 这个东西,大家都熟悉,就简单的介绍一下,算是一个抛砖引玉,因为我自己在Log日志的搭建方面,没有什么经验,但这东西确实是非常重要的,日后调Bug没有它基本不可能,如果有朋友有什么比较好的L ...

  4. c# .net获取文件夹下的所有文件(多层递归),并获取区间数据(Jsion,xml等数据)写出到处理文件,学习分享~

    static void Main(string[] args)        { string path = string.Format(@"C:\Users\Administrator\D ...

  5. Java网络编程--简单聊天程序

    背景 毕业设计前的练手,学校小比赛中的一个题目. 开发环境 Java(eclipse)+Mysql 简介 使用Java+Mysql开发以个简单的聊天工具,在本次项目中实现了: 1. 用户登录(客户端至 ...

  6. Android SDK升级后报错error when loading the sdk 发现了元素 d:skin 开头无效内容

    把错误位置的devices.xml这个文件删除,再把sdk里面tools\lib下的这个文件拷贝到你删除的那个文件夹里,重启eclipse

  7. gprs/gsm 在linux下的应用

    之前有篇随笔整理了一种在裸机下用状态机+超时的机制来操作gprs/gsm,linux下就不需要了,本身有完善的调度机制,在等待的流程中直接sleep就行了. 下面是飞凌OK6410下的 demo, 其 ...

  8. .gitignore过滤个人配置

    git还是一个很好使用的版本工具.所以用eclipse做自己的小玩意儿,在多台电脑之间同步的时候我经常会使用它.. 但是有个问题..不同电脑的eclipse的个人配置稍微有那么一点点的不同..比如有几 ...

  9. spring jdbc 查询结果返回对象、对象列表

    首先,需要了解spring jdbc查询时,有三种回调方式来处理查询的结果集.可以参考 使用spring的JdbcTemplate进行查询的三种回调方式的比较,写得还不错. 1.返回对象(queryF ...

  10. 集中日志服务器Rsyslog

    http://www.gaizaoren.org/archives/408 基于主机的管理一般需要收集服务器的日志信息用于及时发现错误,处理故障. 搭建linux下的集中日志服务器的程序一般可以用sy ...