【wpf学习】如何继承自定义控件?

本文介绍如何在WPF应用程序中自定义Tab控件,通过创建继承自UserControl的ITabPage类,并在主项目中添加用户控件实现继承。文章提供了XAML和CS代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【前言】

近日,由于要做一个代码生成器(-----为什么我一直都在做这个?),我打算在主界面上面铺一个tab控件,然后每一个自定义控件都继承一个ITabPage的标签页父类,便于调用,但是搜索网上无果,捣鼓之后发现继承也并非不可以。

首先,请自定义一个父类,我这里定义了这个东西(在另一个项目EBaseUI下面):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace EBaseUI
{
    public class ITabPage :UserControl
    {
        
    }
}

ok,现在我们就在主项目里添加一个用户控件,然后改写一下继承自定义控件:

xaml代码:

<local:ITabPage x:Class="WPFCodeGen.Modules.ConnMgr"
             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" 
             xmlns:local="clr-namespace:EBaseUI;assembly=EBaseUI"
             RenderOptions.BitmapScalingMode="NearestNeighbor"
             d:DesignHeight="600" d:DesignWidth="800">
    <Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="79*" />
                <RowDefinition Height="116*" />
                <RowDefinition Height="405*" />
            </Grid.RowDefinitions>
            <StackPanel  Name="stackPanel1" Orientation="Horizontal" >
                <Menu Background="{x:Null}">
                    <MenuItem Header="添加" Padding="16,0,0,0" Margin="0,1,5,1">
                        <MenuItem.Background>
                            <ImageBrush ImageSource="/WPFCodeGen;component/resources/toolStripAdd.Image.png" Stretch="None" AlignmentX="Left"></ImageBrush>
                        </MenuItem.Background>
                    </MenuItem>

                    <MenuItem Header="编辑" Padding="16,0,0,0" Margin="0,1,5,1">
                        <MenuItem.Background>
                            <ImageBrush ImageSource="/WPFCodeGen;component/resources/toolStripEdit.Image.png" Stretch="None" AlignmentX="Left"></ImageBrush>
                        </MenuItem.Background>
                    </MenuItem>
                    <MenuItem Header="保存" Padding="16,0,0,0" Margin="0,1,5,1">
                        <MenuItem.Background>
                            <ImageBrush ImageSource="/WPFCodeGen;component/resources/toolStripSave.Image.png" Stretch="None" AlignmentX="Left"></ImageBrush>
                        </MenuItem.Background>
                    </MenuItem>
                    <MenuItem Header="取消" Padding="16,0,0,0" Margin="0,1,5,1">
                        <MenuItem.Background>
                            <ImageBrush ImageSource="/WPFCodeGen;component/resources/toolStripCEL.Image.png" Stretch="None" AlignmentX="Left"></ImageBrush>
                        </MenuItem.Background>
                    </MenuItem>                    
                </Menu>
               
                

            </StackPanel>
        </Grid>
    </Grid>
</local:ITabPage>

cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using EBaseUI;
namespace WPFCodeGen.Modules
{
    /// <summary>
    /// ConnMgr.xaml 的交互逻辑
    /// </summary>
    public partial class ConnMgr : EBaseUI.ITabPage
    {
        public ConnMgr()
        {
            InitializeComponent();
        }
    }
}

有几点要注意:

local这个前缀必须在 xaml头部定义:

xmlns:local="clr-namespace:EBaseUI;assembly=EBaseUI"


这里定义的是要引用到的项目。

local:ITabPage 表示 EBaseUI下面的ITabPage类。

x:Class="WPFCodeGen.Modules.ConnMgr"

上面这一行表示这个xaml代码对应的类是 WPFCodeGen.ModulesConnMgr,听说,xmal前台代码与后台cs代码会被编译到同一个文件类里面。

ok,大功告成。

WPF中添加自定义控件通常涉及以下几个步骤: 1. **创建UserControl或CustomControl类**:首先,创建一个新的类,继承自`UserControl`或`Control`,如果需要更复杂的控件结构,可以选择`Panel`作为基础。例如: ```csharp public partial class MyCustomControl : UserControl { public MyCustomControl() { InitializeComponent(); } } ``` 2. **设计用户界面**:在你的新类中,使用XAML来定义控件的布局和外观。使用`<StackPanel>`、`<Grid>`等元素,并为它们设置属性和事件处理器。 ```xml <UserControl x:Class="MyNamespace.MyCustomControl"> <Grid> <!-- 控件内容 --> <TextBlock Text="这是一个自定义控件"/> </Grid> </UserControl> ``` 3. **初始化控件**: 在`InitializeComponent()`方法中,可以设置控件的初始状态和默认行为。 4. **公开属性和方法**:为了让外部能够定制你的控件,可以在`public`部分声明属性和方法,如依赖属性、命令等。 5. **注册控件**:为了在WPF中可以直接使用你的自定义控件,需要在应用程序启动时将其注册。在App.xaml.cs中添加`RegisterCustomControl`方法: ```csharp public static void RegisterCustomControl() { var assembly = typeof(MyCustomControl).Assembly; var type = assembly.GetType("MyNamespace.MyCustomControl"); var resourceDictionary = new ResourceDictionary { Source = new Uri($"pack://application:,,,/{type.Assembly.GetName().Name};component/MyCustomControl.xaml", UriKind.RelativeOrAbsolute) }; Application.Current.Resources.MergedDictionaries.Add(resourceDictionary); } ``` 然后在`App.xaml`的`Startup`钩子中调用这个方法。 现在你就可以在其他XAML文件中像使用内置控件一样引用你的自定义控件了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值