wpf的UserControl用户控件怎么添加到Window窗体中
转载自 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窗体中的更多相关文章
- WPF 精修篇 用户控件
原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...
- WPF之路——用户控件对比自定义控件UserControl VS CustomControl)
将多个现有的控件组合成一个可重用的“组”. 由一个XAML文件和一个后台代码文件. 不能使用样式和模板. 继承自UserControl类. 自定义控件(扩展) 在现有的控件上进行扩展,增加一些新的属性 ...
- WPF学习- AllowDrop 用户控件启用拖放功能
知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) ...
- [WPF 学习] 3.用户控件库使用资源字典的困惑
项目需要(或者前后端分离的需要),前端我使用了用户控件库,由后端用代码加载和控制. 然而用户控件库没法指定资源字典,于是在用户控件的xaml文件里面手工添加了资源字典 <UserControl. ...
- WPF窗口和用户控件事件相互触发
问题1: WPF项目里有一个窗口和一个用户控件,窗口和用户控件里都有一个Button,点击窗口里的Button如何触发用户控件里Button的Click事件 解答: //窗口代码 public par ...
- WPF获取当前用户控件的父级窗体
方式一.通过当前控件名获取父级窗体 Window targetWindow = Window.GetWindow(button); 方式二.通过当前控件获取父级窗体 Window parentWind ...
- Winform 后台将指定的控件集合添加到制定容器中
/// <summary> /// 把按钮按照行数分割排列 /// </summary> /// <param name="ControlArry"& ...
- 【demo练习四】:WPF用户控件案例
首先,新建vs中“用户控件(WPF)”,右键项目名 =>"添加"按钮 => 选择“新建项”. 然后选择“用户控件(WPF)” => 起名字 => 点击“添加 ...
- 用户控件(UserControl)
简介 "用户控件"继承自UserControl,而UserControl继承自ContentControl,也就是内容控件UserControl和Window是一个层次上的,都有x ...
随机推荐
- Redis之个人简单理解
1.什么是redis? 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.M ...
- js正则匹配过滤 特殊字符
function stripscript(s) { var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<& ...
- 深入理解JSX
本文由笔者翻译自官方文档的JSX In Depth,若干案例经过了改写.其实说白了也好像不算太深入,但还是提示了一些可能的盲区. JSX是为构造React元素方法React.createElement ...
- thinkphp 3.2 join
$res2 = M('stat_info a') ->join(C('DB_PREFIX').'stock b ON a.goods_id = b.goods_id') ->field(' ...
- Vue.js 整理笔记
以前我们用Jquery进行dom的操作,虽然熟悉后开发效率很高,但是如果多个控件的相互操作多的情况下,还是会乱.相比之下,Vue的使用更加清晰,通过虚拟dom将数据绑定,而且组件化和路由的帮助下,让整 ...
- mybatis中#{}与${}的差别(如何防止sql注入)
默认情况下,使用#{}语法,MyBatis会产生PreparedStatement语句中,并且安全的设置PreparedStatement参数,这个过程中MyBatis会进行必要的安全检查和转义. # ...
- [转]Oracle10g数据库自动诊断监视工具(ADDM)使用指南
第一章 ADDM简介 在Oracle9i及之前,DBA们已经拥有了很多很好用的性能分析工具,比如,tkprof.sql_trace.statspack.set even ...
- 关于es5的一些新方法
1.数组方法(1)isArray在之前我们判断数组类型的数据都是用instanceof来判断的,es5新增了对数组的判断,即Array.isArray()(2)every和some这两个方法一般用于对 ...
- 一些js
//fixed块随滚动条滚动 window.onscroll=function(){ var scroll_left = $(window).scrollLeft(); $('#table_fixed ...
- 数据库阿里连接池 druid配置详解
http://blog.csdn.net/hj7jay/article/details/51686418 http://lj6684.iteye.com/blog/1770093 http://www ...