【问题标题】:How to assign control template to window in Wpf?如何将控件模板分配给 Wpf 中的窗口?
【发布时间】:2012-11-16 12:03:09
【问题描述】:

我正在尝试定义一个控件模板,然后我想将其用于模式对话框。问题是,我遵循了在 stackoverflow 和其他任何地方可以找到的所有说明,但是没有加载和应用样式/模板?相反,我得到一个静态资源异常。

那么,如果模板和窗口位于不同的文件中,如何将模板应用到我的窗口?

有什么帮助吗?

<Window x:Class="WpfWindowTemplateTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Template="{StaticResource MyWindowTemplate}"
        Style="{StaticResource MyWindowStyle}" />

我使用的模板是这个:

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

    <ControlTemplate x:Key="MyWindowTemplate" TargetType="{x:Type Window}">
        <Border x:Name="WindowBorder" Style="{DynamicResource WindowBorderStyle}">
            <Grid>
                <Border Margin="4,4,4,4" Padding="0,0,0,0" x:Name="MarginBorder">
                    <AdornerDecorator>
                        <ContentPresenter/>
                    </AdornerDecorator>
                </Border>
                <ResizeGrip Visibility="Collapsed" IsTabStop="false" HorizontalAlignment="Right" x:Name="WindowResizeGrip" 
                    VerticalAlignment="Bottom" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
                    <Condition Property="WindowState" Value="Normal"/>
                </MultiTrigger.Conditions>
                <Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/>
                <Setter Property="Margin" TargetName="MarginBorder" Value="4,4,4,20" />
            </MultiTrigger>
            <Trigger Property="WindowState" Value="Maximized">
                <Setter Property="CornerRadius" TargetName="WindowBorder" Value="0,0,0,0"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <Style x:Key="MyWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="AllowsTransparency" Value="False" />
        <Setter Property="WindowStyle" Value="SingleBorderWindow" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="ShowInTaskbar" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border>
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    
</ResourceDictionary>

【问题讨论】:

  • 确切的错误是什么,这个 ResourceDictionary 在哪里加载?

标签: c# wpf templates controltemplate


【解决方案1】:

使用 DynamicResource 代替 StaticResource。

<Window x:Class="WpfWindowTemplateTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Template="{DynamicResource MyWindowTemplate}"
    Style="{DynamicResource MyWindowStyle}" />

将此添加到您的 app.xaml

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/WpfWindowTemplateTest;component/MyWindowTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

【讨论】:

  • 也不起作用。然后窗口保持黑色。这就是为什么我现在为它疯狂了几个小时。
  • 奇怪,我已经能够以这种方式将 ControlTemplate 设置到我的主窗口:/
  • 我编辑了我的答案:您需要在 app.xaml 中引用您的 xaml;在旁注中,黑色是因为您将 AllowsTransparency 设置为 false。
  • 谢谢。现在工作。但是,我仍然无法摆脱黑色背景。无论我为背景颜色或允许透明度设置什么。
  • Matthias,这是一个非常好的问题。我自己也遇到了这个“黑色”事情的问题,并试图追查它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多