无法在样式中放置窗口 (WPF) [英] Can't put a window in a style (WPF)

查看:33
本文介绍了无法在样式中放置窗口 (WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种感觉,我错过了一些非常重要的东西.我创建了以下代码:

I have the feeling that I am missing something very important. I've created the following code:

文件:App.xaml

File: App.xaml

<Application x:Class="HelloWorld.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
    <Application.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MainView.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>
</Application>

文件:MainWindow.xaml

File: MainWindow.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:viewmodel="clr-namespace:HelloWorld.ViewModel">
    <DataTemplate DataType="{x:Type viewmodel:ViewModel}">
        <Window Title="HelloWorld">
            <Window.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="Margin" Value="50"></Setter>
                </Style>
            </Window.Resources>
            <TextBlock Text="TODO" />
        </Window>
    </DataTemplate>
</ResourceDictionary>

虽然我可以编译代码,一切看起来都不错.我收到了从 <Window Title="HelloWorld"></Window> 的灰色摆动线,并显示Can't put a Window in a Style".".我想知道我做错了什么?

Although I can compile the code and everything looks good. I get the grey wiggly lines from <Window Title="HelloWorld"> to </Window> with the message "Can't put a Window in a Style.". I am wondering what did I do wrong?

我应该怎么做才能改进我的代码?顺便说一句,我正在尝试使用 MVVM.

What should I do to improve my code? I am btw trying to use the MVVM.

谢谢!

推荐答案

Window:

提供创建、配置、显示和管理窗口和对话框生命周期的能力.

Provides the ability to create, configure, show, and manage the lifetime of windows and dialog boxes.

和:

主要用于显示独立应用程序的窗口和对话框.

is primarily used to display windows and dialog boxes for standalone applications.

由于 Window 元素是顶级元素,它们不能被添加到低级元素的 Content 中.您的 Can't put a Window in a Style 错误很明显...您不能在 StyleWindow 中使用 Windowcode>DataTemplate 在你的情况下.

As Window elements are top-level elements, they cannot be added into the Content of lower level elements. Your Can't put a Window in a Style error is clear... you cannot use a Window in a Style, or in a DataTemplate in your case.

与其尝试这样做,您还有几个选择:

Instead of attempting that, you have a couple of choices:

1) 将 Window 内容放入 DataTemplate,然后在 Window 中的 ContentControl 中显示该内容>:

1) Put the Window contents into a DataTemplate and then display that content in a ContentControl in a Window:

<DataTemplate DataType="{x:Type viewmodel:ViewModel}">
    <!-- Define content -->
</DataTemplate>

...

<ContentControl Content="{Binding ViewModelProperty}" />

2) 使用 UserControl 而不是 Window 元素:

2) Use a UserControl instead of a Window element:

<DataTemplate DataType="{x:Type viewmodel:ViewModel}">
    <UserControl>
        <!-- Define Content here -->
    </UserControl>
</DataTemplate>

这篇关于无法在样式中放置窗口 (WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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