【发布时间】:2010-06-09 06:45:43
【问题描述】:
我正在尝试创建一个自定义控件,该控件将在链接下方显示一个带有一些文本的超链接按钮。这个想法是让紧急消息显示在 Silverlight 页面的屏幕上。根据我的阅读,我认为我应该能够创建一个新控件,然后创建一些依赖属性并将组件的动态部分绑定到它们,以便允许我将自定义控件的多个实例添加到我的银光项目。这是我定义控件的 XAML
<UserControl
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"
mc:Ignorable="d"
x:Class="WhatsNew.UrgentStoryGridControl"
d:DesignWidth="608" d:DesignHeight="65" Background="White">
<UserControl.Resources>
<Style x:Key="WhatsNewTitleStyle" TargetType="HyperlinkButton">
Removed for Brevity
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Height="65" Margin="0" VerticalAlignment="Bottom" Background="White">
<StackPanel>
<HyperlinkButton Style="{StaticResource WhatsNewTitleStyle}" Content="{Binding linkText}" HorizontalAlignment="Left" VerticalAlignment="Top" NavigateUri="{Binding linkURI}" Foreground="Red"/>
<TextBlock Style="{StaticResource WhatsNewTextStyle}" Text="{Binding storyText}" Margin="0,13,0,0" d:LayoutOverrides="Height"/>
</StackPanel>
</Grid>
在后面的代码中,我创建了三个依赖属性
Partial Public Class UrgentStoryGridControl
Inherits UserControl
Public Shared linkTextProperty As DependencyProperty = DependencyProperty.Register("linkText", GetType(String), GetType(UrgentStoryGridControl), New PropertyMetadata("Link Text"))
Public Shared linkURIProperty As DependencyProperty = DependencyProperty.Register("linkURI", GetType(String), GetType(UrgentStoryGridControl), New PropertyMetadata("link.html"))
Public Shared storyTextProperty As DependencyProperty = DependencyProperty.Register("storyText", GetType(String), GetType(UrgentStoryGridControl), New PropertyMetadata("Story Text"))
Public Property linkText() As String
Get
Return GetValue(linkTextProperty)
End Get
Set(ByVal value As String)
SetValue(linkTextProperty, value)
End Set
End Property
Public Property linkURI() As String
Get
Return GetValue(linkURIProperty)
End Get
Set(ByVal value As String)
SetValue(linkURIProperty, value)
End Set
End Property
Public Property storyText As String
Get
Return GetValue(storyTextProperty)
End Get
Set(ByVal value As String)
SetValue(storyTextProperty, value)
End Set
End Property
End Class
当我使用 Expression Blend 将此控件放置在我的 Silverlight 项目上时,我看到属性窗口的“杂项”部分中列出了三个属性,正如我所期望的那样。 PropertyMetadata 中的值被填充为这些属性的默认值。这是我的 Silverlight 项目中的代码,我保留了默认值:
<local:UrgentStoryGridControl x:Name="urgentStory" Height="65" />
这是我尝试将值设置为某物的代码:
<local:UrgentStoryGridControl x:Name="urgentStory" Height="65" linkText="Test Link Text" linkURI="testpage.html" storyText="Sample Story Text" />
无论我尝试使用该控件,启动应用程序时都没有显示任何内容。我认为我遗漏了一些小东西,但是在今天花了很多时间研究这个之后,我没有找到任何可以表明我遗漏或做错了什么的东西。
【问题讨论】:
标签: silverlight custom-controls