wpf usercontrol封装公共基类

  1. 创建普通类而不是usercontrl,然后继承usercontrol
public abstract class PureBaseUserControl : UserControl
{

}
  1. 修改子类的usercontrol继承上面的基类(需要改两处)
    将子类的前台代码最外侧的改为上面的基类(注意下方的wpfinherit:PureBaseUserControl)
<wpfinherit:PureBaseUserControl  xmlns:wpfinherit="clr-namespace:WPFInherit" x:Class="WPFInherit.TestControl"
             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="450" d:DesignWidth="800">
    <Grid>
        <TextBlock x:Name="txtShow"/>
    </Grid>
</wpfinherit:PureBaseUserControl>

再调整子类的后台代码继承基类

public partial class TestControl : PureBaseUserControl
{

}
posted @ 2021-11-17 14:18  Hey,Coder!  阅读(452)  评论(0)    收藏  举报