Matthew
Matthew

Reputation: 67

applying set Window style to window

I have defined a custom Windows control template inside Libraries\WindowsTemplates:

 <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>

I have referenced the ControlTemplate in App.xaml:

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

I try to use the template in my window:

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}">

And still I get the error that the resource cannot be resolved. What am I missing?

My entire app.xaml looks like this:

<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>

Upvotes: 0

Views: 48

Answers (1)

Rekshino
Rekshino

Reputation: 7325

Put all your ResourceDictionary's to the property ResourceDictionary of 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>

Upvotes: 1

Related Questions