原文 [WPF疑难] 继承自定义窗口

[WPF疑难] 继承自定义窗口

周银辉

项目中有不少的弹出窗口,按照美工的设计其外边框(包括最大化,最小化,关闭等按钮)自然不同于Window自身的,但每个弹出框的外边框都是一样的。对其中一个窗口而言,我们要取消其Window边框,并在右上角摆上三个按钮并编写其点击事件等,但若每个弹出窗口都按照这种方式做一遍就太土了。我们想避免重复劳动,最自然的联想到了“继承”。但WPF给我们找了若干麻烦,被挫败了几次。今天经过2小时的奋战,终于搞定了,分享一下。

挫败1,继承时编译错误

假设我们写好的父窗口类为BaseWindow,对应BaseWindow.cs和BaseWindow.xaml, 要继承它的窗口为Window1,对应Window1.cs和Window1.xaml,我们常常进行的动作是将VS为我们自动生成的代码中的如下语句:

public partial class Window1 : Window

修改成:

public partial class Window1 : BaseWindow

但编译后,你会得到一个错误:Window1有着不同的基类。

这是因为在window1.xaml中

<Window

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="InheritWindowDemo.Window1"

Width="300" Height="300">

<Grid x:Name="LayoutRoot"/>

</Window>

我们的Window继承了Window类,打开Window1.g.cs也可以看到这一点(这是VS自动生成的一个中间文件,可以在Window1的InitializeComponent()方法上“转到定义”来跳转到该文件,也可以在Obj"Debug目录下找到)。这就使得我们的Window1同时继承Window和BaseWindow类,多继承是不被允许的。

那么自然地,需要修改Window1.xaml,将其中的根“Window”,修改成我们的BaseWindow:

<src:BaseWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="InheritWindowDemo.Window1"

xmlns:src="clr-namespace:InheritWindowDemo"

Height="300"

Width="300">

<Grid>

</Grid>

</src:BaseWindow>

心想,这下可以编译通过了吧,抱歉,不行,又得到另一个编译错误:src:BaseWindow不能是Xaml文件的根,因为它是由Xaml定义的,目前我避免这个问题的办法是让BaseWindow仅仅在C#中定义(即,没有BaseWindow.xaml,只有BaseWindow.cs)。

OK,编译顺利通过,继承成功。

挫败2,外边框(包括最小化,最大化和关闭按钮)放在哪里

明显,不能作为BaseWindow的内容,这是因为继承了BaseWindow的子类窗口(比如Window1)会覆盖BaseWindow的内容。

假设BaseWindow这样编写:

public BaseWindow()

{

Grid grid = new Grid();

Button minBtn = new Button();

Button maxBtn = new Button();

Button closeBtn =new Button();

//something to ini these buttons

grid.Children.Add(minBtn);

grid.Children.Add(maxBtn);

grid.Children.Add(closeBtn);

this.Content = grid;

}

当子类Window1如下定义时:

<src:BaseWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="InheritWindowDemo.Window1"

xmlns:src="clr-namespace:InheritWindowDemo"

Height="300"

Width="300">

<Grid>

<TextBlock Text="hi , i am window1"/>

</Grid>

</src:BaseWindow>

这样以来Window1中的Grid和TextBlock会覆盖BaseWindow的内容而仅仅看到“hi,I am window1”的文本块而没有最小化最大化以及关闭按钮了。

事实上,我们应该反过来想,Window也是一个控件,与其他控件一样其外观及其外观中的视觉元素仍然是由其Style和ControlTemplate来定义的。想到这里,一切就变得简单了,我们应该将窗口外边框(包括最小化,最大化和关闭按钮)定义在其Template中,其他一些属性(比如是否支持透明等)定义在Style中

其Template如下:

<ControlTemplate x:Key="BaseWindowControlTemplate" TargetType="{x:Type Window}">

<DockPanel LastChildFill="True">

<!--外边框-->

<Border Width="Auto"

Height="Auto"

DockPanel.Dock="Top"

Background="#FF7097D0"

CornerRadius="4,4,0,0"

x:Name="borderTitle">

<StackPanel HorizontalAlignment="Right"

Orientation="Horizontal">

<!--最小化按钮-->

<Button Content="Min"

x:Name="btnMin" />

<!--最大化按钮-->

<Button Content="Max"

x:Name="btnMax" />

<!--关闭按钮-->

<Button Content="Close"

x:Name="btnClose" />

</StackPanel>

</Border>

<Border Background="{TemplateBinding Background}"

BorderBrush="{TemplateBinding BorderBrush}"

BorderThickness="{TemplateBinding BorderThickness}"

Width="Auto"

Height="Auto"

DockPanel.Dock="Top"

CornerRadius="0,0,4,4">

<AdornerDecorator>

<ContentPresenter />

</AdornerDecorator>

</Border>

</DockPanel>

</ControlTemplate>

其Style如下:

<Style x:Key="BaseWindowStyle"

TargetType="{x:Type Window}">

<Setter Property="Template" Value="{StaticResource BaseWindowControlTemplate}"/>

<Setter Property="AllowsTransparency"

Value="True" />

<Setter Property="WindowStyle"

Value="None" />

<Setter Property="BorderBrush"

Value="#FF7097D0" />

<Setter Property="BorderThickness"

Value="4,0,4,4" />

<!—Something else-->

</Style>

然后在BaseWindow的构造函数中指定其Style为我们定义的样式:

private void InitializeStyle()

{

this.Style = (Style) App.Current.Resources["BaseWindowStyle"];

}

这样一来,所有继承了BaseWindow的窗体,都有我们统一定义的外观了。

挫败3,让外边框(包括最小化,最大化和关闭按钮)响应事件

只有外观还不够,至少得有鼠标事件吧。那最小化事件来说,要做的事情是找到定义在ControlTemplate中的btnMin这个Button控件,然后当其被点击时该ControlTemplate被应用到的那个窗体被最小化。

FrameworkTemplate.FindName(string name, FrameworkElement templatedParent)方法可以做帮助我们找到指定的FrameworkTemplate被应用到templatedParent上后具有name名称的控件。

ControlTemplate baseWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"];

Button minBtn = (Button)baseWindowTemplate.FindName("btnMin", this);

minBtn.Click += delegate

{

this.WindowState = WindowState.Minimized;

};

其他事件同理:)不过值得提醒的是,上面这样的代码应该在窗体的Style和Template被应用之后,比如你可以在Loaded后编写使用上面的代码而不是直接放在构造方法中,否则FrameworkTemplate.FindName()方法将返回null。

至此,问题搞定。下载DEMO:http://files.cnblogs.com/zhouyinhui/InheritWindowDemo.zip

[WPF疑难] 继承自定义窗口的更多相关文章

  1. [WPF疑难]如何禁用窗口上的关闭按钮

    原文 [WPF疑难]如何禁用窗口上的关闭按钮 [WPF疑难]如何禁用窗口上的关闭按钮                                           周银辉 哈哈,主要是调用Rem ...

  2. WPF学习笔记-自定义窗口

    代码部分 <Style x:Key="for_noresize_window" TargetType="{x:Type Window}"> < ...

  3. WPF自定义窗口基类

    WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义.继承自定义窗口的类xaml文件的根节点就不再是<Window>,而是自定义窗口类名(若自定义窗口与继承者不在同一个命名 ...

  4. WPF 之 创建继承自Window 基类的自定义窗口基类

    开发项目时,按照美工的设计其外边框(包括最大化,最小化,关闭等按钮)自然不同于 Window 自身的,但窗口的外边框及窗口移动.最小化等标题栏操作基本都是一样的.所以通过查看资料,可按如下方法创建继承 ...

  5. WPF自学入门(九)WPF自定义窗口基类

    今天简单记录一个知识点:WPF自定义窗口基类,常用winform的人知道,winform的窗体继承是很好用的,写一个基础窗体,直接在后台代码改写继承窗体名.但如果是WPF要继承窗体,我个人感觉没有理解 ...

  6. WPF绘制自定义窗口

    原文:WPF绘制自定义窗口 WPF是制作界面的一大利器,下面就用WPF模拟一下360的软件管理界面,360软件管理界面如下: 界面不难,主要有如下几个要素: 窗体的圆角 自定义标题栏及按钮 自定义状态 ...

  7. wpf 自定义窗口,最大化时不覆盖任务栏

    相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 WindowState="Maximized& ...

  8. WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)

    无论是在工作和学习中使用WPF时,我们通常都会接触到CustomControl,今天我们就CustomWindow之后的一些边角技术进行探讨和剖析. 窗口(对话框)模态闪动(Blink) 自定义窗口的 ...

  9. [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)

    原文 [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu) [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu) 周银辉 点击窗口左上角图标时弹出来的菜单也就是这里所说的系 ...

随机推荐

  1. Github实例教程-创建库、创建主页

    以README文件为实例,具体介绍github的使用过程 请先下载git,然后配置下面内容: ( 我的系统是debian,其它版本号的UNIX/Linux有区别),windows的临时不清楚. (一) ...

  2. CodeIgniter框架开发的统计程序源代码开放

    文章来源: PHP开发学习门户 自己初学php时,用CodeIgniter框架开发的后台统计程序源代码 程序部分页面如图: 具体配置及下载源代码:http://bbs.phpthinking.com/ ...

  3. objective-C学习笔记(八) 集合类型 Collection Types

    OBJC的集合类型: 1.数组 Array 2.Set 3.键值对 Dictionary 数组:OC中的数组被定义为class,引用类型.索引从0开始,访问越界会抛出运行时异常. NSArray的元素 ...

  4. Sql日期时间格式转换 备用

    sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...

  5. php如何开启GD库

    GD库是干什么用的呢!它是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片.GD库在php中默认是没有开启的,如果想让它支持图片处理功能,那么就要手 ...

  6. [LeetCode]题解(python):084-Largest Rectangle in Histogram

    题目来源: https://leetcode.com/problems/largest-rectangle-in-histogram/ 题意分析: 给定一个数组,数组的数字代表这个位置上的bar的高度 ...

  7. java从c struct传来的字节数组中取值

    public int getInt(byte[] array,int index) { return (array[index]  & 0xff)  | (array[index + 1] & ...

  8. inline-block 垂直居中

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 关于LD_DEBUG (转载)

    引用 LD_DEBUGThe dynamic library loader used in linux (part of glibc) has some neat tricks. One of the ...

  10. QBoxLayout::setSizeConstraint可以固定窗口的大小,且根据内部控件所占用的位置自动调节大小

    setSizeConstraint(QLayout::SetFixedSize)的功能是保持整个对话框的尺寸保持相对固定.也就是说让布局管理器来调整对话框的大小.举个例子吧:一个上下可扩展对话框,基本 ...