0

Im having a wierd problem with a WPF (+CaliburnMicro) listBox as defined below

    <GroupBox Header="Configurations" Width="Auto" Grid.Row="1" Grid.Column="2">
        <ListBox ItemsSource="{Binding SelectableConfigurations}" BorderThickness="0" Width="Auto">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding Path=Name}" IsChecked="{Binding Path=IsSelected}" Margin="15,3" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>
    </GroupBox>

It appears that when i select an item in the list it modifies the item in the bound list but not in other places the item is referenced. IE the below code updates the item in the _selectableConfigurations list but not the original object as attached to the _sc list.

It appears that when the property was modified the original object was replaced in the list rather than simply updated. Is this the case?

    private readonly List<SelectableConfiguration> _selectableConfigurations;
    private ISelectableConfigurations _sc;

    public ConfigurationTabViewModel(
        ISelectableConfigurations configurations)
    {
        _sc = configurations;
        _selectableConfigurations = configurations.SelectableConfigurations.ToList();
    }

    public List<SelectableConfiguration> SelectableConfigurations
    {
        get { return _selectableConfigurations; }
    }

EDIT

 public class SelectableConfiguration
    {
        public bool IsSelected { get; set; }
        public string Name { get; set; }
        public IRunableOrmConfiguration Configuration { get; set; }
    }
H.B.
  • 166,899
  • 29
  • 327
  • 400
undefined
  • 33,537
  • 22
  • 129
  • 198
  • 1
    Also post the declaration of `SelectableConfiguration` please. – Clemens Aug 03 '12 at 12:12
  • What about `configurations.SelectableConfigurations.ToList()`? Is that [Enumerable.ToList](http://msdn.microsoft.com/en-us/library/bb342261.aspx) or your own implementation that perhaps creates new list elements from the old ones? – Clemens Aug 03 '12 at 12:16
  • Definitely enumerable ToList which uses references, see http://stackoverflow.com/questions/2774099/tolist-does-it-create-a-new-list – undefined Aug 03 '12 at 12:18
  • What's the implementation of the `SelectableConfigurations` property like? That's the only place I can imagine that might make copies of your `SelectableConfiguration` objects. – Alex Humphrey Aug 03 '12 at 12:23

1 Answers1

1

Your item probably gets edited just fine but the rest of the UI does not know about it because the item does not "say anything". Implement INotifyPropertyChanged in the item class.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Nah its not that, I look in the debugger later and one collection has the value set the other doesn't. Its got to be because they are different instances in each list – undefined Aug 03 '12 at 12:20
  • @LukeMcGregor: I have never encountered any unseen copying so far (which in itself is a pretty difficult thing if a class is more complex), if you really have different instances it's probably somewhere in your own code. – H.B. Aug 03 '12 at 12:22
  • Hmm you were right, it only happens when the collection is IEnumerable and the UI is the first to enumerate it, still not sure why this is though. If i put a tolist when i initialise the collection it all works as expected – undefined Aug 03 '12 at 12:46
  • That sounds like the problem might be with the `IEnumerator` implementation of your custom collection. – H.B. Aug 03 '12 at 13:55