随笔 - 58  文章 - 1  评论 - 2  阅读 - 38646 

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();
        }
    }
}

  

posted on   花开花落-2014  阅读(2429)  评论(1编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示