WPF继承窗口 或控件

WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义。继承自定义窗口的类xaml文件的根节点就不再是<Window>,
而是自定义窗口类名(若自定义窗口与继承者不在同一个命名空间,还得加上命名空间),继承自定义窗口类后台代码也得修改为继承自自定义窗口
exp:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//继承Window类的自定义窗口类
namespace WPF_Study.Entity
{
   using System.Windows;
 
   public class WindowBase:Window
    {
        private const int Fixed_Width = 540;
        private const int Fixed_Height = 350;
 
        public WindowBase() : base()
        {
            this.MaxWidth = Fixed_Width;
            this.MaxHeight = Fixed_Height;
            this.MinWidth = Fixed_Width;
            this.MinHeight = Fixed_Height;
        }
    }
}
 
//继承自定义窗口
//xaml文件
<localEntity:WindowBase x:Class="WPF_Study.TestWindowBase"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:localEntity="clr-namespace:WPF_Study.Entity"
        Title="TestWindowBase" >
    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Width" Value="60"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="Margin" Value="5"/>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="40*"/>
            <RowDefinition Height="221*"/>
        </Grid.RowDefinitions>
        <WrapPanel Grid.Row="0" Grid.Column="0">
            <Button Content="查询" x:Name="btnSearch"></Button>
            <Button Content="新增" x:Name="btnAdd"></Button>
            <Button Content="修改" x:Name="btnAmend"></Button>
            <Button Content="删除" x:Name="btnDelete"></Button>
        </WrapPanel>
        <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Grid.Row="2" Grid.Column="0" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="窗口编号" Binding="{Binding Win}"/>
                <DataGridTextColumn Header="评价器地址" Binding="{Binding Evalutor}"/>
                <DataGridTextColumn Header="条屏地址" Binding="{Binding StripeScreen}"/>
                <DataGridTextColumn Header="IP" Binding="{Binding IP}"/>
                <DataGridTextColumn Header="注册设备号" Binding="{Binding RegNum}"/>
                <DataGridTextColumn Header="描述" Binding="{Binding Description}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</localEntity:WindowBase>
//对应后台代码
using WPF_Study.Entity;
 
namespace WPF_Study
{
    /// <summary>
    /// Interaction logic for TestWindowBase.xaml
    /// </summary>
    public partial class TestWindowBase : WindowBase
    {
        public TestWindowBase()
        {
            InitializeComponent();
        }
    }
}

  

展开阅读全文

没有更多推荐了,返回首页