views:

162

answers:

1

I am trying to create a custom control that will display a hyperlink button with some text below the link. The idea is to have urgent messages show up on a screen of a Silverlight page. From what I have read, I thought that I should be able to create a new control and then create some dependancy properties and bind the dynamic parts of the component pieces to them in order to allow me to add multiple instances of the custom control to my Silverlight project. Here is my XAML that defines the control

<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>

In the code behind, I have created three dependancy properties

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

When I place this control on my Silverlight project using Expression Blend, I see the three properties listed in the Miscellaneous section of the properties window as I would expect. The values from the PropertyMetadata are populated as the default values for these properties. Here is the code from my Silverlight project where I leave the default values alone:

<local:UrgentStoryGridControl x:Name="urgentStory" Height="65"  />

Here is the code where I try to set the values to something:

<local:UrgentStoryGridControl x:Name="urgentStory" Height="65" linkText="Test Link Text" linkURI="testpage.html" storyText="Sample Story Text" />

Either way I attempt to use the control, I'm not getting anything displayed when I launch the application. I figure that I'm missing something small but after having spent a lot of time today researching this, I'm not finding anything that would indicate what I'm missing or doing wrong.

A: 

You need to set the DataContext in your custom UserControl or else your bindings won't work.

In your UrgentStoryGridControl's constructor, you should be able to set Me.DataContext = Me

David.Poll
David,Thanks for the clarification. I don't know why I hadn't seen this documented anywhere but I just tried your suggestion and it works exactly as I want it to.
BMcBride