Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF collections Binding
I've got the following two combo boxes. One updates the content when the SortedList changes and the other doesn't.
<ComboBox Name="cbxNotUpdating"
  ItemsSource="{Binding Path=DataContext.Versions.Values, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
  SelectedValue="{Binding Path=Version, Mode=TwoWay}">
</ComboBox>
 
<ComboBox Name="cbxUpdating"
  ItemsSource="{Binding Path=DataContext.Versions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
    SelectedValue="{Binding Path=Version, Mode=TwoWay}">
</ComboBox>
 
The obvious difference is that cbxNotUpdating binds to the Values and cbxUpdating binds to the SortedList itself.
My List implementation looks like this:
public class VersionList : SortedList<string, Version>, INotifyCollectionChanged
{
...
  public event NotifyCollectionChangedEventHandler CollectionChanged;
  protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
  {
      if (CollectionChanged != null)
      {
          CollectionChanged(this, e);
      }
  }
}
How can I make the cbxNotUpdating update?
I've got a property that is a 'Version' that I want to have as the SelectedValue at the same time I have a template that works on just the Value part of the collection.
 
I'm using vs2010.
Posted 20-Jan-12 6:01am
Erik Rude4.2K
Edited 20-Jan-12 7:15am
v2
Comments
SteveAdey at 20-Jan-12 11:29am
   
The difference is that the Values are just an IList which has no mechanism to notify of its changes.
 
What are you trying to achieve with cbxNotUpdating that you cannot with cbxUpdating?
Erik Rude at 20-Jan-12 12:17pm
   
Thanks fo rthe reply - I've got a property that is a 'Version' that I want to have as the SelectedValue at the same time (this doesn't work in the cbxUpdating) I have a template that works on just the Value part of the collection. I refer to that just on the DataType eg:

Erik Rude at 20-Jan-12 12:21pm
   
I will try to work around this then. I just thought that the Values would automatically refresh when the collection did.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Thanks to SteveAdey for pointing out the fatal flaw.
In the end I bound to the SortedList changed the SelectedValuePath and used a DataTemplate binding to the Value to get to my desired goal.
My solution looks like this:
 
<ComboBox Name="cbxFolder" Grid.Column="1" Grid.Row="0"
                ItemsSource="{Binding Path=DataContext.Versions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                SelectedValue="{Binding Path=Version, Mode=TwoWay}"
                SelectedValuePath="Value"
                ComboBox.ItemTemplate="{StaticResource VersionCbxTemplate}">
</ComboBox>
With this DataTemplate in the resources
<DataTemplate x:Key="saturnVersioncbx">
  <StackPanel Margin="5" DataContext="{Binding Value}" >...
  </StackPanel>
</DataTemplate>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 Maciej Los 270
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 202
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 23 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100