用户控件(UserControl)

简介

"用户控件"继承自UserControl,而UserControl继承自ContentControl,也就是内容控件
UserControl和Window是一个层次上的,都有xaml和cs文件

流程

创建用户控件

1

写好用户控件

<UserControl x:Class="WpfDemo.UserControlDemo.OwnUserControl"
             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>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Content="这是一个用户控件"></Label>
        <Slider Minimum="0" Maximum="100" Grid.Row="1"></Slider>
    </Grid>
</UserControl>

用户控件也可以套用用户控件,组成更复杂的界面

<UserControl x:Class="WpfDemo.UserControlDemo.UseUserControlUserControl"
             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"
             xmlns:userControlDemo="clr-namespace:WpfDemo.UserControlDemo"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Content="这是另一个用户控件,在这里使用了OwnUserControl用户控件"></Label>
        <userControlDemo:OwnUserControl Grid.Row="1"></userControlDemo:OwnUserControl>
        <Label Grid.Row="2" Content="这个用户控件在OwnUserControl的基础上再添加一个Slider"></Label>
        <Slider Grid.Row="3" Minimum="0" Maximum="200" ></Slider>
    </Grid>
</UserControl>

窗体引用用户控件

<Window x:Class="WpfDemo.UserControlDemo.UseUserControlWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:userControlDemo="clr-namespace:WpfDemo.UserControlDemo"
        mc:Ignorable="d"
        Title="UseUserControlWindow" Height="600" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.2*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="0.2*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Content="使用OwnUserControl用户控件"></Label>
        <userControlDemo:OwnUserControl Grid.Row="1"></userControlDemo:OwnUserControl>
        <Label Grid.Row="2" Content="使用UseUserControlUserControl用户控件"></Label>
        <userControlDemo:UseUserControlUserControl Grid.Row="3"></userControlDemo:UseUserControlUserControl>
    </Grid>
</Window>

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/UserControlDemo

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录 什么是WPF? WPF的历史? 为什么要用WPF及WPF作用 WPF与winForm区别? 什么是WPF? ...
    灬52赫兹灬阅读 11,146评论 2 11
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,416评论 25 709
  • “美好的青春是感情的喷薄期。无论在生理上,还是在心理上,都有种莫名的悸动。一种燥热感,不期而遇,如洪水猛兽,势不可...
    雅俗共赏Y阅读 2,444评论 0 1
  • 文 | 厦九九 1. 多少人面临择业,总是纠结和迷茫,尤其是刚毕业和毕业头几年的年轻人。都说谁的青春不迷茫,但迟迟...
    厦九九阅读 5,775评论 17 56