WPF : How is this code working?

Started by
7 comments, last by maya18222 11 years, 6 months ago
Taken from one of the MSDN examples : http://msdn.microsoft.com/en-us/library/aa970558.aspx

I don't understand how the information in the lower three textboxes of the ContentControl stays synced up with the current selection in the list box. The binding for the ContentControl is bound to an ObservableCollection, which doesnt have any "CurrentItem" functionality. So what is the "IsSynchronizedWithCurrentItem" doing to make this all work?
Advertisement

<StackPanel>
<TextBlock FontFamily="Verdana" FontSize="11"
Margin="5,15,0,10" FontWeight="Bold">My Friends:</TextBlock>
<ListBox Width="200" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource MyFriends}}"/>
<TextBlock FontFamily="Verdana" FontSize="11"
Margin="5,15,0,5" FontWeight="Bold">Information:</TextBlock>
<ContentControl Content="{Binding Source={StaticResource MyFriends}}"
ContentTemplate="{StaticResource DetailTemplate}"/>
</StackPanel>


But the ListBox must have a some sort of CurrentItem functionality. Actually I'm sure of it. In Webforms, WinForms, controls like Dropdowns and Listboxes have a property to determine what item was selected. So I can't fathom that the WPF/Silverlight Listbox would not have that same functionality.

Beginner in Game Development?  Read here. And read here.

 

WPF list boxes inherit from a class called "Selector". Selector, I believe, has a has-a relationship with an object of a class called "Items" or something like that. Items has a CurrentItem property; setting IsSynchronizedWithCurrentItem to true tells WPF to keep this CurrentItem property in sync with the listbox selection.
Yes but both the ContentControl and the listbox are being bound to an ObservableCollection. All the ContentControl sees is the ObservableCollection. How is it somehow seeing the current item in the listbox/Selector/Items.
Oh, I see what you are asking... I don't know the answer and don't have time right now to research it. But what events does ObservableCollection expose? Could it be firing an event on selection that both the ContentControl and the ListBox implementations are listening to?
It works because an ICollectionView has the CurrentItem property and all collections have a default view instance that can be accessed by calling GetDefaultView(). So, when you bind a regular collection, such as an ObservableCollection, then WPF automatically binds to its default view (see Using a default View).

It works because an ICollectionView has the CurrentItem property and all collections have a default view instance that can be accessed by calling GetDefaultView(). So, when you bind a regular collection, such as an ObservableCollection, then WPF automatically binds to its default view (see Using a default View).


Still not following ... there's two controls, a listbox and a content control which are bound to an observable collection as their Content and ItemSource properties respectively.

Are you saying that the ObservableCollection has an instance of some object that instantiates the ICollectionView interface and uses this object, by default, to track the current item in the ObservableCollection? -- so in essence the ObservableCollection has a current item: it's just stored in some other object? Because ObservableCollection itself isn't an ICollectionView ... it isn't any kind of view; it's a viewmodel right?

so in essence the ObservableCollection has a current item: it's just stored in some other object?

Yes pretty much. Associated 1-to-1 with your ObservableCollection is a default view. In this example your collection gets bound twice (once to a listbox and once to a content control) and so both times the same implicit view object is used.
Ah, I see. I didnt realise it was doing stuff behind the scenes like that.

This topic is closed to new replies.

Advertisement