转载自 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. Linux 建立文件夹的链接

    linux下的软链接类似于windows下的快捷方式 建立软链接 ln -s a b a 就是源文件,b是链接文件名,其作用是当进入b目录,实际上是链接进入了a目录 example:ln -s /ho ...

  2. PHP 进程间通信——消息队列(msg_queue)

    PHP 进程间通信--消息队列 本文不涉及PHP基础库安装.详细安装说明,请参考官网,或期待后续博客分享. 1.消息队列函数准备 <?php//生成一个消息队列的key$msg_key = ft ...

  3. 3 django系列之Form表单在前端web界面渲染与入库保存

    author: 温柔易淡(Leo),欢迎技术交流与拍砖 preface 我们在前端写表单的时候,其实可以使用django自带的forms功能来实现,特别是在处理 修改已经存在数据 的场景特别好用,下面 ...

  4. AngularJS版本下载

    大家可以从下面地址获取AngularJS所以版本: https://code.angularjs.org/

  5. tomcat 动态部署

    还可以在conf目录下依次创建Catalina\localhost目录,然后在localhost目录下为 test 这个Web应用程序建立 test.xml 文件,编辑这个文件输入以下内容 从Tomc ...

  6. Casual Note

    20170104 冯诺依曼计算机(遵循冯诺依曼结构设计的计算机:存储器.运算器.控制器.输入设备.输出设备)之前也有计算机,不过在那之前的计算机是专用的,不可编程,只能干特定的事情没法干其他事.与之前 ...

  7. CSS--background

    它的组合写法: background-color, background-image, background-repeat,backgroundattachment, background-posit ...

  8. SpringMVC学习记录4

    主题 SpringMVC有很多很多的注解.其中有2个注解@SessionAttributes @ModelAttribute我平时一般不用,因为实在是太灵活了.但是又有一定限制,用不好容易错.. 最近 ...

  9. 【11-10】spring学习笔记-ApplicationContextAware

    package util; /** * @author aloha_world_ * @date 2016年11月10日 下午7:50:08 * @version v1.00 * @descripti ...

  10. Intellij Idea/Webstorm/Phpstorm 的高效快捷键

    1. shift + F6可以理解为F2的豪华重量版,不但可以重命名文件名,而且可以命名函数名,函数名可以搜索引用的文件,还可以重命名局部变量.还可以重命名标签名.在sublime text中有个类似 ...