Sign in to follow this  

WPF : How is this code working?

This topic is 1905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[CODE]
<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>
[/CODE]

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. Edited by Alpha_ProgDes

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
It works because an [url="http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx"]ICollectionView[/url] has the CurrentItem property and all collections have a default view instance that [url="http://msdn.microsoft.com/en-us/library/ms752284.aspx"]can be accessed[/url] by calling [url="http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.getdefaultview.aspx"]GetDefaultView()[/url]. So, when you bind a regular collection, such as an ObservableCollection, then WPF automatically binds to its default view (see [url="http://msdn.microsoft.com/en-us/library/ms752347.aspx#how_to_create_a_view"]Using a default View[/url]). Edited by dmatter

Share this post


Link to post
Share on other sites
[quote name='dmatter' timestamp='1348263614' post='4982513']
It works because an [url="http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx"]ICollectionView[/url] has the CurrentItem property and all collections have a default view instance that [url="http://msdn.microsoft.com/en-us/library/ms752284.aspx"]can be accessed[/url] by calling [url="http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.getdefaultview.aspx"]GetDefaultView()[/url]. So, when you bind a regular collection, such as an ObservableCollection, then WPF automatically binds to its default view (see [url="http://msdn.microsoft.com/en-us/library/ms752347.aspx#how_to_create_a_view"]Using a default View[/url]).
[/quote]

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? Edited by jwezorek

Share this post


Link to post
Share on other sites
[quote name='jwezorek' timestamp='1348266689' post='4982523']
so in essence the ObservableCollection has a current item: it's just stored in some other object?[/quote]
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.

Share this post


Link to post
Share on other sites

This topic is 1905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this