Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've got the following two combo boxes. One updates the content when the SortedList changes and the other doesn't.
XML
<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:
C#
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
Updated 20-Jan-12 6:15am
v2
Comments
SteveAdey 20-Jan-12 11:29am    
The difference is that the Values are just an IList<tvalue> which has no mechanism to notify of its changes.

What are you trying to achieve with cbxNotUpdating that you cannot with cbxUpdating?
Erik Rude 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:
<datatemplate x:key="VersionTemplate" datatype="{x:Type MyApp:Version}" xmlns:x="#unknown">
Erik Rude 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

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:

XML
<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
XML
<DataTemplate x:Key="saturnVersioncbx">
  <StackPanel Margin="5" DataContext="{Binding Value}" >...
  </StackPanel>
</DataTemplate>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900