我是靠谱客的博主 标致故事,这篇文章主要介绍【wpf学习】如何继承自定义控件?,现在分享给大家,希望可以做个参考。

【前言】

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

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; namespace EBaseUI { public class ITabPage :UserControl { } }

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

xaml代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<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代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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头部定义:

复制代码
1
xmlns:local="clr-namespace:EBaseUI;assembly=EBaseUI"


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

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

复制代码
1
x:Class="WPFCodeGen.Modules.ConnMgr"

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

ok,大功告成。

最后

以上就是标致故事最近收集整理的关于【wpf学习】如何继承自定义控件?的全部内容,更多相关【wpf学习】如何继承自定义控件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部