tags:

views:

1714

answers:

1

I've got two Silverlight Controls in my project, both have properties TeamId. I would like to bind these together in XAML in the control hosting both user controls similar to:

        <agChat:UserTeams x:Name="oUserTeams" />
        <agChat:OnlineUser x:Name="oOnlineUsers" TeamId="{Binding ElementName=oUserTeams, Path=TeamId}" />

In the first control, I'm implementing System.ComponentModel.INotifyPropertyChanged and raising the PropertyChanged event upon the TeamId property changing.

In the second control, I've used the propdp snippet to identify the TeamId as a Dependency property.

        // Using a DependencyProperty as the backing store for TeamId.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TeamIdProperty = 
        DependencyProperty.Register(
        "TeamId", 
        typeof(string), 
        typeof(OnlineUsers), 
        new System.Windows.PropertyMetadata(new System.Windows.PropertyChangedCallback(TeamChanged)));

However when the silverlight controls first gets created, I get the follow exception from Silverlight:

Unhandled Error in Silverlight 2 Application Invalid attribute value {Binding ElementName=oUserTeams, Path=TeamId} for property TeamId. [Line: 21 Position: 146] at System.Windows.Application.LoadComponent(Object component, Uri xamlUri) at agChat.Page.InitializeComponent() at agChat.Page..ctor() at agChat.App.Application_Startup(Object sender, StartupEventArgs e) at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Any ideas what I'm doing wrong? Obviously this could all be done in code-behind, but this seems like the correct approach.

Kevin

+3  A: 

That is the correct approach in WPF, but not in Silverlight.

You cannot bind to elements using xaml in Silverlight.

This is the offending line: TeamId="{Binding ElementName=oUserTeams, Path=TeamId}"

Specificly ElementName

If you can, place the data object into Resources and declare it there, then you can do this:

<agChat:UserTeams x:Name="oUserTeams" 
       DataContext="{StaticResource myDataObject}" />
<agChat:OnlineUser x:Name="oOnlineUsers" 
       DataContext="{StaticResource myDataObject}" 
       TeamId="{Binding  TeamId}" />
Brian Leahy
I stumbled upon this today. Thanks for the great suggestion! (Looking forward to Element binding support in SL3!)
Kevin Babcock