Silverlight 3 doesn't display the default template for a custom control I'm working on.
I have three projects in my solution:
- CustomControl.Controls - Silverlight Class Library
- CustomControl.Silverlight - Silverlight Application
- CustomControl.Silverlight.Web - Web Application
In CustomControl.Controls I have the following class:
[TemplateVisualState(Name = "Normal", GroupName = "FocusStates")]
public class SampleControl : ContentControl
{
public SampleControl()
{
DefaultStyleKey = typeof(SampleControl);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
UpdateVisualState(false);
}
void UpdateVisualState(bool useTransitions)
{
VisualStateManager.GoToState(this, "Normal", useTransitions);
}
}
Themes/generic.xaml is configured as Embeded Resource and contains the following:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="controls:SampleControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:SampleControl">
<Border Background="Orange" CornerRadius="5" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Finally I'm using the custom control inside MainPage.xaml in CustomControl.Silverlight:
<UserControl x:Class="CustomControl.Silverlight.MainPage"
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:sample="clr-namespace:CustomControl.Controls;assembly=CustomControl.Controls"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<StackPanel x:Name="LayoutRoot">
<sample:SampleControl Width="100" Height="200" />
<Button Width="100" Height="200" Content="bar" />
</StackPanel>
</UserControl>
In the browser the SampleControl isn't visible (it still occupies 200px in height, so it is there) and below it, a button is displayed.
I'm using Visual Studio 2008 SP1 + Silverlight 3 Tools.
Is there anything else I need to do so that the template defined in Themes/generic.xaml is applied to SampleControl?
Thanks