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

【前言】

近日,由于要做一个代码生成器(-----为什么我一直都在做这个?),我打算在主界面上面铺一个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,大功告成。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值