首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Wpf中将控件模板分配给窗口?

如何在Wpf中将控件模板分配给窗口?
EN

Stack Overflow用户
提问于 2012-11-16 20:03:10
回答 1查看 6.8K关注 0票数 1

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

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

有什么帮助吗?

代码语言:javascript
复制
<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}" />

我使用的模板是这个:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-16 20:08:00

使用DynamicResource而不是StaticResource。

代码语言:javascript
复制
<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

代码语言:javascript
复制
<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/WpfWindowTemplateTest;component/MyWindowTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13416238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档