0
votes

I have a usercontrol I have created. It is used within the DataTemplate of a list box. The listbox is bound to an observable collection of custom objects.

I need a way to pass the current object to my usercontrol via binding.

I have tried to create a public property on the user control as such:

public TypedMediaItem CurrentItem { get; set; }

And set the binding parameters:

<my:LargeStreamItemControl Height="160" Width="295" CurrentItem="{Binding }" />

However this gives me the following error:

Object of type 'System.Windows.Data.Binding' cannot be converted to type 'F1MediaCentre.Classes.Payload.Typed.TypedMediaItem'.

I am unsure where I am going wrong - I am new to Silverlight, having inherited a live production application from a former colleague, and am racking my brains with this one.

1

1 Answers

3
votes

I'll take a stab at this... it is possible that you can't bind to it because it is not a dependency property.

Add this to your user control in place of your current CurrentItem:

    Public Shared ReadOnly CurrentItemProperty As DependencyProperty = _
    DependencyProperty.Register("CurrentItem", GetType(TypedMediaItem), GetType(LargeStreamItemsControl), New PropertyMetadata(Nothing))
    Public Property CurrentItem() As TypedMediaItem
        Get
            Return DirectCast(GetValue(LargeStreamItemsControl.CurrentItemProperty), TypedMediaItem)
        End Get
        Set(value As TypedMediaItem)
            SetValue(LargeStreamItemsControl.CurrentItemProperty, value)
        End Set
    End Property

Sorry I do mostly VB... hope you can convert to C# :)

This will make it a dependency property and therefore you'll be able to bind to it.

Okay, here's C#:

public static readonly DependencyProperty CurrentItemProperty = 
DependencyProperty.Register("CurrentItem", typeof(TypedMediaItem), typeof(LargeStreamItemsControl), new PropertyMetadata(null));
public TypedMediaItem CurrentItem 
{
    get { return (TypedMediaItem)GetValue(LargeStreamItemsControl.CurrentItemProperty); }
    set { SetValue(LargeStreamItemsControl.CurrentItemProperty, value); }
}