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

I'm new to WPF, so this makes me gabble.

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 defined the Template that he used as follows (the idea was literally just for 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 that I defined basically does the same:

    <!--<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 click Debug in Visual Studio I get the following IF error and only if the button has a style or template applied to it.

Without a style or template, the window loads fine. enter image description here

EDIT

On request, here is 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>

      

+3


source to share


2 answers


You must define <Window.Resources>

before the mesh. 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>

      

+8


source


You have to put Window.Resources

up Grid

in your XAML.

StaticResource

is MarkupExtension

, and it goes and gets the resource correctly when XAML parses it, so the order in which it appears in the XAML matters a lot. No forward declarations; the resource must already be defined.



From MSDN :

The StaticResource should not attempt to make a direct reference to a resource that is lexically defined in the XAML file. Attempting to do this is not supported, and even if such a link does not fail, attempting a direct link will incur a load time penalty when looking up internal hash tables that represent the ResourceDictionary. For best results, adjust your resource vocabularies so that backlinks are avoided. If you can't avoid direct reference, use the DynamicResource markup extension instead.

+2


source







All Articles