1
votes

Scratching my brain here....

I have the following ListBox

    <ListBox Height="221" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="Auto" ItemsSource="{Binding MediaItems}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid ShowGridLines="True">
                    <my:MediaItemControl CurrentItem="{Binding}" />

                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListBox>

The listbox is bound to an ObservableCollection of a object.

As you can see this ListBox includes a user control. Within the user control I have the following dependancy property information.

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

What I am trying to do is pass the current item within the ItemsSource of the ListBox, to my usercontrol. However using the above methods doesnt work, the setter on the dependency property is never called.

What am I doing wrong?

1
Just to be clear, the MediaItems is an ObservableCollection<TypedMediaItem> and you are sure that the IemsSource binding is correctly bound to the collection. Is this correct? Also, how do you know the dependency property's setter is never called?(Be specific)Bryan Watts
Yes MediaItems is an ObservableCollection<TypedMediaItem> and I am sure that the IemsSource binding is correctly bound to the collection, as if I replace the user control with a label, I can directly bind it to members of the TypedMediaItem object. I know that the setter is never called as I set a breakpoint on the set method of the property which is never it.Mick Walker

1 Answers

3
votes

Anything that binds to a DependencyProperty will not directly call the CLR property that you created. To actually know whether the DependencyProperty is changed, you need to create a PropertyChangedCallback as shown below.

//Never used by xaml that binds to CurrentItem
//Only used when referenced directly by C# code
//Example: (obj as MediaItemControl).CurrentItem = new TypedMediaItem();
public TypedMediaItem CurrentItem{
    get { return (TypedMediaItem) GetValue(CurrentItemProperty ); }
    set { SetValue(CurrentItemProperty , value); }
}

//Used by xaml binding
public static readonly DependencyProperty CurrentItemProperty =
   DependencyProperty.Register("CurrentItem", typeof(TypedMediaItem),
   typeof(MediaItemControl), new PropertyMetadata(null, new PropertyChangedCallback(OnCurrentItemChanged)));

private static void OnCurrentItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
    //TODO: Set break point here
    //If this code is executed, then the property was changed
    MediaItemControl instance = (MediaItemControl) d;
    TypedMediaItem newValue = (TypedMediaItem) e.NewValue;
    return;
}

This doesn't answer your question exactly, but should help you debug a little farther. If you still can't figure out what is wrong, then I'm going to more information because everything that you have provided looks correct to me. For example, the error you are getting or more code on the MediaItemControl. You could also upload the project somewhere and I could look at it.