将样式应用于WPF中的自定义窗口 [英] Apply Style to custom Window in WPF

查看:137
本文介绍了将样式应用于WPF中的自定义窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义窗口:



Hi, I have a custom window:

public class MyWindow : Window
{
   ...
}





和此窗口的单独样式:





and a separate style for this window:

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

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

                    xmlns:Component="clr-namespace:MyControls.Component">

    <!-- Style -->
    <Style x:Key="ExWindowStyle" TargetType="{x:Type Component:MyWindow}">
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="WindowState" Value="Maximized" />
    </Style>
</ResourceDictionary>





此代码不要''工作,我有一个例外,因为它不能创建组件:MyWindow。有任何想法吗 ?谢谢!

推荐答案

您的代码帖子没有​​显示足够的细节,知道您在做什么以及如何但我可以给您一些一般的指示。



如果你正在创建一个真正的自定义控件,那么在你的帖子中你跳过的课程中有很多管道工程是一切工作的关键。具体来说,静态类构造函数需要:



DefaultStyleKeyProperty.OverrideMetadata(typeof(MyWindow),new FrameworkPropertyMetadata(typeof(MyWindow)));



没有它,就元数据而言,你的类仍然是窗口。除非类元数据正确,否则xaml解析引擎无法将xaml中声明的类型与类类型匹配。但这只是自定义控件的开始。您还需要覆盖OnApplyTemplate并创建一些其他样板方法,然后才能编写自定义方法。



其次,对于自定义控件,默认样式必须在themes / generic.xaml中声明。其他任何地方和解析引擎都找不到它。在那里声明的样式不能有x:键。它会导致解析错误。此外,您需要定义模板或声明模板基于另一个模板,否则将没有视觉效果。自定义控件不会继承其父级的视觉效果。它们应该以默认样式声明。



然而,我很好奇为什么你需要创建一个窗口的自定义后代正在做的是覆盖两个属性。您最好只创建一个派生样式并设置这两个属性,然后通过附加到Window类或在应该应用的窗口上明确指定它们来应用。创建自定义控件的唯一原因是添加新行为或以其他方式修改现有行为,否则您无法使用xaml中已有的样式/模板功能。



如果您有充分的理由进行自定义控件,我即将发布一个由三部分组成的关于在WPF中编写自定义控件的系列文章。如果您愿意,我可以向您发送文章的高级副本。
Your code post doesn''t show enough detail to know what exactly you are doing and how but I can give you some general directions.

If you are creating a true custom control, then there is a lot of plumbing that is in the class which you skipped over in your post but which is key to making it all work. Specifically, the static class constructor needs:

DefaultStyleKeyProperty.OverrideMetadata(typeof(MyWindow), new FrameworkPropertyMetadata(typeof(MyWindow)));

Without that, your class is still "Window" as far as the metadata goes. The xaml parsing engine can''t match up the type declared in the xaml with the class type unless the class metadata is correct. But that is only the start for a custom control. You also need to override OnApplyTemplate and create some other boilerplate methods before you can even get to the point of writing your custom methods.

Secondly, for a custom control, your default style must be declared in themes/generic.xaml. Anywhere else and the parsing engine can''t find it. The style declared in there cannot have an x:key. It will cause a parsing error. Further, you need to define a template or declare that the template is based on another template otherwise there will be no visual. Custom controls don''t inherit the visuals of their parent. They are expected to be declared in the default style.

However, I''m curious why you need to create a custom descendent of a window when it seems all you are doing is overriding two properties. You would be WAY better off just creating a derived style and setting those two properties then applying by attaching to the Window class or explicitly specifying them on the windows they should apply to. The only reason to create a custom control is to add new behavior or otherwise modify existing behavior you can''t otherwise get with the styling/templating features already in xaml.

If you have a good reason for a custom control, I''m about to publish a three-part series on writing custom controls in WPF. I can send you an advanced copy of the articles if you would like.


这篇关于将样式应用于WPF中的自定义窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆