6

I want to a property to the current item of a ICollectionView how can I do it? The ICollectionView is used for binding to a combo box, how can I bind another control to the ICollectionView's selected item?

Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

3 Answers3

9

Check out this cheat sheet. In particular, check out the / binding symbol, which references the current item in a collection view.

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
  • That works great, I noticed that if I change the current item on the view source, the value doesn't update. Do I need a dependency property for this? Or maybe for this, I should use @Rachel's method? – Jiew Meng Nov 03 '10 at 03:15
  • 1
    @jiewmeng, see John's response; you'll want to use IsSynchronizedWithCurrentItem. – Dan Bryant Nov 03 '10 at 13:27
7

Setting IsSynchronizedWithCurrentItem on the ComboBox will update the current item with its selection (not sure if you're already doing this). You can then bind the same collection and access its current item with the binding:

<ComboBox ItemsSource="{Binding Names}" IsSynchronizedWithCurrentItem="True" />
<Button Content="{Binding Path=Names/}"/>
John Bowen
  • 24,213
  • 4
  • 58
  • 56
3

Give your ComboBox a name and bind to it's SelectedItem.

For example:

<ComboBox x:Name="MyComboBox" ItemsSource="{Binding MyList}" />

<Grid DataContext={Binding ElementName=MyComboBox, Path=SelectedItem>
...
</Grid>
Rachel
  • 130,264
  • 66
  • 304
  • 490