WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义。继承自定义窗口的类xaml文件的根节点就不再是<Window>,
而是自定义窗口类名(若自定义窗口与继承者不在同一个命名空间,还得加上命名空间),继承自定义窗口类后台代码也得修改为继承自自定义窗口
exp:
//继承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(); } } }
时间: 2025-01-18 07:54:26