普通用户控件使用

我们来新建一个用户控件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);
        }
    }
复制代码
复制代码

 

posted @   jeffery1010  Views(228)  Comments(0Edit  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示