WPF 自定义UserControl

本文介绍了如何在WPF项目中创建一个名为MyTimePicker的自定义UserControl,用于小时和分钟的增减调节。通过提供XMAL和后台代码实现详细步骤,并展示了如何在项目中引用和使用该控件。编译后,可以在工具箱中找到MyTimePicker,方便快捷地添加到窗口中进行布局和操作。
摘要由CSDN通过智能技术生成

有时候需要按照需求创建自定义的控件,避免重复编码,下面演示如何添加UserConrol,如果在项目中引用创建好的UserControl。

1. 创建一个项目,右键添加UserControl(WPF)

给用户控件重命名,本例中重命名为“MyTimePicker”

在这里插入图片描述

该控件的主要功能,实现小时,分钟的增减调节功能。

完整界面XMAL代码如下:

<UserControl x:Class="WpfApp.MyUserControls.MyTimePicker"
            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:local="clr-namespace:WpfApp.MyUserControls"
            mc:Ignorable="d"
            Background="White"
            d:DesignHeight="450" d:DesignWidth="800">
    <Grid>

        <Grid.Resources>
            <Style TargetType="RepeatButton">
                <Setter Property="Margin" Value="5"/>
            </Style>
        </Grid.Resources>

  
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <RepeatButton x:Name="btnIncreaseHour"
                Content="+"
                FontSize="24"
                Click="btnIncreaseHour_Click" Interval="100"></RepeatButton>
        <RepeatButton x:Name="btnDecreaseHour" Content="-"
                      FontSize="24"
                      Grid.Row="1"
                      Click="btnDecreaseHour_Click"
                      Interval="100"></RepeatButton>
        <RepeatButton x:Name="IncreaseMins"
                     Content="+"
                      FontSize="24"
                      Grid.Column="2"
                      Click="IncreaseMins_Click"
                      Interval="100"></RepeatButton>

        <RepeatButton x:Name="DecreaseMins"
                      Content="-"
                     FontSize="24"
                      Grid.Column="2"
                     Grid.Row="1"
                      Click="DecreaseMins_Click"
                      Interval=" 100"></RepeatButton>

        <TextBox x:Name="txtTimePicker"
                FontSize="24"
                FontWeight="DemiBold"
                Foreground="Blue"
                Grid.Column="1"
                Grid.RowSpan="2"
                VerticalAlignment="Center"
                TextAlignment="Center"/>
    </Grid>
</UserControl>

在这里插入图片描述

后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;

using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp.MyUserControls

{

    /// <summary>

    /// MyTimePicker.xaml 的交互逻辑

    /// </summary>

    public partial class MyTimePicker : UserControl
    {
        public MyTimePicker()
        {
            InitializeComponent();
            txtTimePicker.Text = $"{hour:00}:{min:00}";
        }

        int hour = DateTime.Now.Hour;
        int min = DateTime.Now.Minute;
        private void btnIncreaseHour_Click(object sender, RoutedEventArgs e)
        {
            hour++;
            if(hour==24)
            {
                hour = 0;
            }
            txtTimePicker.Text = $"{hour:00}:{min:00}";
        }

        private void btnDecreaseHour_Click(object sender, RoutedEventArgs e)
        {
            hour--;
            if (hour == -1)
            {
                hour = 23;
            }
            txtTimePicker.Text = $"{hour:00}:{min:00}";
        }

        private void IncreaseMins_Click(object sender, RoutedEventArgs e)
        {
            min++;
            if(min==60)
            {
                min = 0;
            }
            txtTimePicker.Text = $"{hour:00}:{min:00}";

        }

        private void DecreaseMins_Click(object sender, RoutedEventArgs e)
        {
            min--;
            if (min == -1)
            {
                min = 59;
            }
            txtTimePicker.Text = $"{hour:00}:{min:00}";
        }
    }
}
2. 编译后,项目添加该控件

编译后在工具箱存在该控件,可以自由添加该控件,和Visual Studio常用控件一样添加。

使用代码布局添加,和使用鼠标拖放一样简单。

XAML代码如下:

<Window x:Class="WpfApp.MainWindow"
        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:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        xmlns:UC="clr-namespace:WpfApp.MyUserControls"
        Title="User Control" Height="450" Width="800">
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Grid.Column="0" Margin="5" Background="LightSkyBlue">
            <TextBlock FontSize="20" HorizontalAlignment="Center">用户自定义控件</TextBlock>
            <TextBlock>演示创建自定义控件</TextBlock>
            <TextBlock>演示如何使用自定义控件</TextBlock>
        </StackPanel>

        <UC:MyTimePicker Grid.Row="0" Grid.Column="1" Margin="5"></UC:MyTimePicker>
        <UC:MyTimePicker Grid.Column="1" HorizontalAlignment="Left" Margin="39,0,0,0" Grid.Row="1" VerticalAlignment="Center"/>
        <UC:MyTimePicker Grid.Column="1" HorizontalAlignment="Left" Margin="230,0,0,0" Grid.Row="1" VerticalAlignment="Center"/>
    </Grid>

</Window>

  

效果如下:

在这里插入图片描述

可以单独对每个控件操作。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF(Windows Presentation Foundation)是一个用于构建Windows应用程序的框架,而UserControlWPF中的一种自定义控件UserControl允许我们将多个现有的WPF控件组合在一起,形成一个新的、可重用的控件。通过创建自定义UserControl,我们可以将一组相关的控件封装成一个单一的控件,以增强应用程序的可维护性和重用性。 创建自定义UserControl通常有以下几个步骤: 1. 创建一个新的WPF用户控件项目,并定义UserControl的外观和布局。这可以通过在XAML文件中使用已有的WPF控件、布局容器和样式来完成。 2. 在UserControl的代码后台(Code-behind)文件中,可以定义一些附加的属性和方法,以增强UserControl的可定制性和功能。 3. 在UserControl中可以定义一些依赖属性(Dependency Properties),以允许开发者在使用UserControl时进行数据绑定和属性设置。 4. 在需要使用自定义UserControl的地方,可以将其直接添加到XAML中,并进行相关的属性设置和事件处理。 自定义UserControl可以在整个应用程序中重复使用,从而提高了开发效率。通过UserControl的封装,我们可以将一组相关的功能和样式打包到单个控件中,简化了应用程序的UI设计和代码开发过程。 总而言之,WPF自定义控件UserControl为开发者提供了一种简单且高效的方式来自定义和组合现有的WPF控件,以创建出更具可重用性和可维护性的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值