Ortund
Ortund

Reputation: 8255

Why won't my window load when I use a style or template on a button?

I'm new to WPF so this is driving me batty.

Consider the following button from my xaml markup:

<Button Name="btnSMS" Click="btnSMS_Click" Height="30" Width="66"
    Margin="10,20,10,10" Background="#FF1E8383" Foreground="White"
    Template="{StaticResource RoundedButtonGreen}">Send SMS</Button>

I've defined the Template that it's using as follows (the idea was literally to just get the rounded corners):

<Window.Resources>
    <ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
        <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
        </Border>
    </ControlTemplate>
</Window.Resources>

In Window.Resources, I also have a Style which I defined that basically does the same thing:

    <!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
        <Setter Property="Background" Value="#FF1E8323" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>-->

So when I hit Debug in Visual Studio, I get the following error IF and only if the button has a Style or Template applied to it.

Without the Style or Template the window loads up perfectly fine. enter image description here

EDIT

Per request, here's the whole window

<Window
        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:SCADA_Demo"
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" xmlns:dxsch="http://schemas.devexpress.com/winfx/2008/xaml/scheduler" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="SCADA_Demo.MainWindow"
        mc:Ignorable="d"
        Title="IHS Towers SCADA" WindowStyle="None" Background="#1e1e1e"
    Loaded="Window_Loaded">

    <Grid>
        <DockPanel LastChildFill="True">
            <!-- Title Bar -->
            <DockPanel LastChildFill="True" DockPanel.Dock="Top" Background="#2d2d30">
                <StackPanel DockPanel.Dock="Left" Height="30" Background="#2d2d30" FlowDirection="LeftToRight" Orientation="Horizontal">
                    <Label Name="lblTitle" HorizontalAlignment="Left" BorderThickness="0" Foreground="White">IHS Towers SCADA</Label>
                </StackPanel>
                <StackPanel DockPanel.Dock="Right" Height="30" Background="#2d2d30" FlowDirection="RightToLeft" Orientation="Horizontal">
                    <Button x:Name="btnClose" Content="X" Click="btnClose_Click" Width="30" Foreground="White"  Background="#2d2d30" BorderThickness="0"></Button>
                    <Button Name="btnMinimize" Content="_" Click="btnMinimize_Click" Width="30" Foreground="White" Background="#2d2d30" BorderThickness="0"></Button>
                </StackPanel>
            </DockPanel>

            <!-- Towers Bar across the top -->
            <StackPanel DockPanel.Dock="Top" Height="50" Background="#2d2d30" VerticalAlignment="Top" x:Name="spTowers" Orientation="Horizontal">
                <Rectangle Name="Tower1" Fill="Green" Height="30" Width="30" Margin="10,5,10,5"></Rectangle>
                <Rectangle Name="Tower2" Fill="Red" Height="30" Width="30" Margin="10,5,10,5"></Rectangle>
            </StackPanel>

            <!-- Alert Window in the center -->
            <StackPanel Name="spBody" DockPanel.Dock="Top" Height="396" Background="#b6bcc6"></StackPanel>

            <!-- Actions Bar across the bottom -->
            <StackPanel DockPanel.Dock="Bottom" Height="60" Background="#2d2d30" VerticalAlignment="Bottom" x:Name="spActions" FlowDirection="RightToLeft" Orientation="Horizontal">

                <Button Name="btnSMS" Click="btnSMS_Click" Height="30" Width="66" Margin="10,20,10,10" Background="#FF1E8383" Foreground="White" Template="{StaticResource RoundedButtonGreen}">Send SMS</Button>
            </StackPanel>
        </DockPanel>
    </Grid>

    <Window.Resources>
        <ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
            <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
            </Border>
        </ControlTemplate>
        <!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
            <Setter Property="Background" Value="#FF1E8323" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>-->
    </Window.Resources>

</Window>

Upvotes: 3

Views: 101

Answers (2)

You should put Window.Resources before the Grid in your XAML.

StaticResource is a MarkupExtension, and it goes and gets the resource right when the XAML parses it -- so order of appearance in the XAML matters a great deal. No forward declarations; the resource must be defined already.

From MSDN:

A StaticResource must not attempt to make a forward reference to a resource that is defined lexically further within the XAML file. Attempting to do so is not supported, and even if such a reference does not fail, attempting the forward reference will incur a load time performance penalty when the internal hash tables representing a ResourceDictionary are searched. For best results, adjust the composition of your resource dictionaries such that forward references can be avoided. If you cannot avoid a forward reference, use DynamicResource Markup Extension instead.

Upvotes: 2

mm8
mm8

Reputation: 169420

You should define <Window.Resources> before the Grid. The order matters:

<Window ...>
    <Window.Resources>
        <ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
            <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
            </Border>
        </ControlTemplate>
        <!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
            <Setter Property="Background" Value="#FF1E8323" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>-->
    </Window.Resources>
    <Grid>
    ...
</Window>

Upvotes: 9

Related Questions