用户控件(UserControl)

 2023-09-05 阅读 135 评论 0

摘要:简介 "用户控件"继承自UserControl,而UserControl继承自ContentControl,也就是内容控件UserControl和Window是一个层次上的,都有xaml和cs文件 流程 创建用户控件 写好用户控件 <UserControl x:Class="WpfDemo.UserControlDemo.OwnUserControl"x

简介

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

流程

创建用户控件

写好用户控件

<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

转载于:https://www.cnblogs.com/Lulus/p/8151405.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/76.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息