WPF自定义窗口在设计时不应用其通用模板 [英] WPF custom window does not apply its generic template at design time

查看:86
本文介绍了WPF自定义窗口在设计时不应用其通用模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的WPF自定义控件,该控件继承自Window,因此以后我可以覆盖它的chrome,并在我的项目中重复使用它.该控件与 generic.xaml 文件(在 Themes 目录中)一起包含在其自己的项目中.控件的样式仅设置红色背景.在第二个项目(相同的解决方案)中,然后将控件用作新WPF窗口的基类.现在,我编译项目并运行它时,它会显示带有所需红色背景的(自定义)窗口.

I have created a simple WPF custom control which inherits from Window, so that later I am able to override it's chrome and reuse it across my projects. The control is contained in its own project along with a generic.xaml file (in the Themes directory). The style for the control simply sets a red background. Within a second project (same solution) I then use the control as the base class of a new WPF window. When I now compile the project and have it run, it shows my (custom-)window with the desired red background.

现在的问题是:如何让 WPF设计器在设计时显示通用样式(在这种情况下为红色背景)?

My question is now: how do I get the WPF designer to show the generic style (in this case the red background) at design time?

当我对按钮做同样的操作时,通用样式将在设计时自动应用.但是,任何继承自Window的控件的默认样式都只会在运行时显示.我想念什么?

When I do the same with, say, a button then the generic style is applied at design time automatically. But any default style of a control inheriting from Window will only show at runtime. What am I missing?

控制代码:

public class CustomControl1 : Window
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }
}

generic.xaml:

generic.xaml:

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="Red"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

使用控件的第二个项目中的测试窗口的XAML:

The XAML of the test window in the second project where I use the control:

<snh:CustomControl1 x:Class="WpfDockWindow.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:snh="clr-namespace:SnH.WpfControls;assembly=SnH.WpfControls"
    Title="TestWindow" Height="300" Width="300">
<Grid>

</Grid>

出于记录目的,我使用VS2013,并且使用框架4.5.1编译了项目.

And for the records, I use VS2013 and the projects are compiled with framework 4.5.1.

Thx,本

推荐答案

我花了很多时间尝试解决此问题.

i have spend a lot of time trying resolve this issue.

答案是:---->将参考项目(控制项目)和消耗项目的平台设置更改为任何Cpu

And the answer has: ----> Change my Platform Settings of the Reference Project (Control Project) and the Consuming Project to be the Any Cpu

如果将控制项目"设置为任何Cpu",而您试图在消费项目"中使用x64,则会出现问题.正确的配置(对不起英语)

If the "Control Project" was setup as "Any Cpu" and you are trying to use in the "Consuming Project" as x64, the issue occours. The Correct Configuration (Sorry about English)

这篇关于WPF自定义窗口在设计时不应用其通用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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