【发布时间】:2009-10-03 02:13:57
【问题描述】:
Silverlight 3 不显示我正在处理的自定义控件的默认模板。
我的解决方案中有三个项目:
- CustomControl.Controls - Silverlight 类库
- CustomControl.Silverlight - Silverlight 应用程序
- CustomControl.Silverlight.Web - 网络应用程序
在 CustomControl.Controls 我有以下类:
[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 被配置为嵌入式资源并包含以下内容:
<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>
最后我在 CustomControl.Silverlight 中使用 MainPage.xaml 中的自定义控件:
<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>
在浏览器中,SampleControl 是不可见的(它仍然占据 200px 的高度,所以它就在那里),在它的下方,会显示一个按钮。
我正在使用 Visual Studio 2008 SP1 + Silverlight 3 工具。
我还需要做什么才能将 Themes/generic.xaml 中定义的模板应用于 SampleControl?
谢谢
【问题讨论】: