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?
Anything that binds to a
DependencyPropertywill not directly call the CLR property that you created. To actually know whether theDependencyPropertyis changed, you need to create aPropertyChangedCallbackas shown below.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.