Button ControlTemplate Exception: Element is already loaded
Posted: 08 Apr 2014, 23:38
Hey all, I'm slowly figuring out xaml and ran into an issue that I'm a bit stumped on. I've got a button control template that I wanted to add text drop shadow for, however I get the exception that the element is already loaded when I add a second ContentPresenter to the template, here's the code that works and below the code that doesn't. Any info would be greatly appreciated, and for reference I was following the info found here: http://mark-dot-net.blogspot.com/2007/0 ... te-in.html
Code: Select all
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GUIResources/ArtTextureAtlas.xaml"/>
<ResourceDictionary Source="GUIResources/IconsTextureAtlas.xaml"/>
<ResourceDictionary Source="GUIResources/InteractTextureAtlas.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ShadowStyle">
<Setter Property="Control.Foreground" Value="LightGray" />
</Style>
<Style x:Key="InformButton" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="GUIFonts/#JandaAmazingGrace"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="Border" Background="{DynamicResource Btn_Cache_Internl_neutral}">
<Grid>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_over}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_down}" />
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="1.0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_over}" />
</Trigger>
<Trigger Property="IsFocused" Value="True" />
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_inactive}" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Page.Resources>
<StackPanel HorizontalAlignment="Center">
<Button Style="{StaticResource InformButton}" Width="225" Height="63">Hello</Button>
<Button Style="{StaticResource InformButton}">World</Button>
<Button Style="{StaticResource InformButton}" FontSize="20">Big Button</Button>
<Button Style="{StaticResource InformButton}" IsDefault="True">Default</Button>
<Button Style="{StaticResource InformButton}" IsEnabled="False">Disabled</Button>
<Button Style="{StaticResource InformButton}" Width="70" Height="30">70 x 30</Button>
<TextBox />
<Button Style="{StaticResource InformButton}" Width="30" Height="30">
<Path Fill="Black" Data="M 3,3 l 9,9 l -9,9 Z" />
</Button>
<Button Width="70" Height="30">70 x 30</Button>
</StackPanel>
</Page>
Code: Select all
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GUIResources/ArtTextureAtlas.xaml"/>
<ResourceDictionary Source="GUIResources/IconsTextureAtlas.xaml"/>
<ResourceDictionary Source="GUIResources/InteractTextureAtlas.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ShadowStyle">
<Setter Property="Control.Foreground" Value="LightGray" />
</Style>
<Style x:Key="InformButton" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="GUIFonts/#JandaAmazingGrace"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="Border" Background="{DynamicResource Btn_Cache_Internl_neutral}">
<Grid>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="contentShadow" Style="{StaticResource ShadowStyle}">
<ContentPresenter.RenderTransform>
<TranslateTransform X="1.0" Y="1.0" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_over}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_down}" />
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="1.0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_over}" />
</Trigger>
<Trigger Property="IsFocused" Value="True" />
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Btn_Cache_Internl_inactive}" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Page.Resources>
<StackPanel HorizontalAlignment="Center">
<Button Style="{StaticResource InformButton}" Width="225" Height="63">Hello</Button>
<Button Style="{StaticResource InformButton}">World</Button>
<Button Style="{StaticResource InformButton}" FontSize="20">Big Button</Button>
<Button Style="{StaticResource InformButton}" IsDefault="True">Default</Button>
<Button Style="{StaticResource InformButton}" IsEnabled="False">Disabled</Button>
<Button Style="{StaticResource InformButton}" Width="70" Height="30">70 x 30</Button>
<TextBox />
<Button Style="{StaticResource InformButton}" Width="30" Height="30">
<Path Fill="Black" Data="M 3,3 l 9,9 l -9,9 Z" />
</Button>
<Button Width="70" Height="30">70 x 30</Button>
</StackPanel>
</Page>