将设置的窗口样式应用于窗口

时间:2019-10-31 10:56:35

标签: wpf xaml templates

我在Libraries \ WindowsTemplates中定义了一个自定义Windows控件模板:

 <ControlTemplate x:Key="DefaultWindowTemplate" TargetType="{x:Type Window}">
    <Border BorderBrush="Red" BorderThickness="2">
    <Grid Margin="3 3 3 3 " Width="auto" Height="auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="75"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="75"/>
        </Grid.RowDefinitions>

        <UniformGrid Rows="1" Columns="1" Height="75" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0" >
            <Rectangle  Stretch="Fill" Fill="{StaticResource SteelBrush_Vert}" Stroke="Black" RenderTransformOrigin="0.5,0.5"/>
        </UniformGrid>

    </Grid>
        </Border>
    </ControlTemplate>

我在App.xaml中引用了ControlTemplate:

 <ResourceDictionary x:Key="WindowTemplates">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Libraries/WindowsTemplates.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

我尝试在窗口中使用模板:

Window x:Class="QCast.Windows.RawMatEntry"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:QCast.Windows"
    mc:Ignorable="d"
    Title="RawMatEntry" Height="450" Width="800"
    Template="{DynamicResource DefaultWindowTemplate}">

仍然出现错误,指出资源无法解析。我想念什么?

我的整个app.xaml看起来像这样:

<Application x:Class="QCast.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:QCast"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="TextBlock.FontSize" Value="14"/>
        <Setter Property="Height" Value="50"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ResourceDictionary x:Key="MyDictionaries">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="SteelBrush.xaml"/>
            <ResourceDictionary Source="FullSteel.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    <ResourceDictionary x:Key="LEDs">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MindFusion.Gauges.Wpf;component/Themes/Leds.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    <ResourceDictionary x:Key="WindowTemplates">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Libraries/WindowsTemplates.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    <ResourceDictionary x:Key="ControlTemplates">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/QCast;component/Libraries/ControlStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Application.Resources>

1 个答案:

答案 0 :(得分:1)

将所有ResourceDictionary放入ResourceDictionary的属性Application.Resources中:

<Application.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="TextBlock.FontSize" Value="14"/>
        <Setter Property="Height" Value="50"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="SteelBrush.xaml"/>
            <ResourceDictionary Source="FullSteel.xaml"/>
            <ResourceDictionary Source="/MindFusion.Gauges.Wpf;component/Themes/Leds.xaml" />
            <ResourceDictionary Source="Libraries/WindowsTemplates.xaml" />
            <ResourceDictionary Source="/QCast;component/Libraries/ControlStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Application.Resources>
相关问题