Jump to content

  • Log In with Google      Sign In   
  • Create Account


WPF : How is this code working?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 maya18222   Members   -  Reputation: 187

Like
2Likes
Like

Posted 20 September 2012 - 02:38 PM

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?

Sponsor:

#2 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Like
0Likes
Like

Posted 20 September 2012 - 05:03 PM

<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.

Edited by Alpha_ProgDes, 20 September 2012 - 05:04 PM.

Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#3 jwezorek   Crossbones+   -  Reputation: 1756

Like
0Likes
Like

Posted 21 September 2012 - 10:45 AM

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.

#4 maya18222   Members   -  Reputation: 187

Like
0Likes
Like

Posted 21 September 2012 - 12:36 PM

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.

#5 jwezorek   Crossbones+   -  Reputation: 1756

Like
0Likes
Like

Posted 21 September 2012 - 01:01 PM

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?

#6 dmatter   Crossbones+   -  Reputation: 3002

Like
0Likes
Like

Posted 21 September 2012 - 03:40 PM

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).

Edited by dmatter, 21 September 2012 - 03:41 PM.


#7 jwezorek   Crossbones+   -  Reputation: 1756

Like
0Likes
Like

Posted 21 September 2012 - 04:31 PM

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?

Edited by jwezorek, 21 September 2012 - 04:32 PM.


#8 dmatter   Crossbones+   -  Reputation: 3002

Like
1Likes
Like

Posted 21 September 2012 - 05:34 PM

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.

#9 maya18222   Members   -  Reputation: 187

Like
0Likes
Like

Posted 23 September 2012 - 03:31 PM

Ah, I see. I didnt realise it was doing stuff behind the scenes like that.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS