我是靠谱客的博主 柔弱蚂蚁,这篇文章主要介绍WPF 继承自定义的控件,现在分享给大家,希望可以做个参考。

 WPF 继承自定义的控件

1.创建一个BaseControl.cs

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace Test.Control { using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; /// <summary> /// Base control class /// </summary> public class BaseControl : UserControl { // Write your common code here } }

注意不能有BaseControl 不能有UI 文件

 

2. 创建WPF user control

a. ConcreteControl.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<myTypes:BaseControl x:Class="Test.Control.ConcreteControl" 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" xmlns:myTypes="clr-namespace:Test.Control"> <Grid> // put you layout code here </Grid> </myTypes:BaseControl>


b. ConcreteControl.xaml.cs

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
namespace Test.Control { /// <summary> /// Interaction logic for ConcreteControl.xaml /// </summary> public partial class ConcreteControl : BaseControl { public ConcreteControl() { InitializeComponent(); } } }


注意替换xmlns:myTypes="clr-namespace:Test.Control"

 


 

最后

以上就是柔弱蚂蚁最近收集整理的关于WPF 继承自定义的控件的全部内容,更多相关WPF内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部