WPF : How is this code working?
#1 Members - Reputation: 183
Posted 20 September 2012 - 02:38 PM
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?
#2 Crossbones+ - Reputation: 3420
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 ![]()
#3 Crossbones+ - Reputation: 1375
Posted 21 September 2012 - 10:45 AM
#5 Crossbones+ - Reputation: 1375
Posted 21 September 2012 - 01:01 PM
#6 Members - Reputation: 1841
Posted 21 September 2012 - 03:40 PM
Edited by dmatter, 21 September 2012 - 03:41 PM.
#7 Crossbones+ - Reputation: 1375
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 Members - Reputation: 1841
Posted 21 September 2012 - 05:34 PM
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.so in essence the ObservableCollection has a current item: it's just stored in some other object?






