Tuesday, March 8, 2011

ListView ObservableCollection Selection

I have ListView with GridView inside (in order to divide ListView for some columns). I bind ObservableCollection of some objects with that ListView.DataContext when main UserControl is loaded. Items show up but when i create two objects which fields have same values (and put them to observable collection), that new items show up in ListView. Problem is that, when user clicks and select one of the new item - both select.

When value of one of mentioned objects changes (and is different) problem doesn't exist, ListView treat them as it should.


XAML code is simple:


<ListView x:Name="listViewSimpleExpression" Grid.Row="1"  
        ItemsSource="{Binding}"  
        Background="Lavender"  
        SelectionMode="Single"
        <ListView.View> 
            <GridView AllowsColumnReorder="false"
                <GridViewColumn Width="Auto"  
                           Header="Column specification"
                      
(...) 
</ListView> 

C# code behind:
            public ObservableCollection<Travelplanet.Import.Itaka.OfferLine.IExpression> _observableInnerExpressions; 
        public ObservableCollection<Travelplanet.Import.Itaka.OfferLine.IExpression> ObservableInnerExpressions 
        { 
            get 
            { 
                if (_observableInnerExpressions == null) 
                { 
                    if (DataContext == null
                    { 
                        return null
                    } 
                    //Get list of simple expressions from multiargument expression from rule 
                    Rule rule = (Rule)DataContext; 
                    MultiargumentExpression multiExpression = (MultiargumentExpression)rule.Expression; 
                    _observableInnerExpressions 
                            = new ObservableCollection<IExpression>(multiExpression.InnerExpressions); 
                } 
                return _observableInnerExpressions; 
            } 
        } 


And method that adds new object to collection:
             ///  <summary> 
        /// COMMAND CREATE PART-EXPRESSION (create simple expression) 
        /// Create part expression button functionality 
        /// </summary> 
        private void CmdCreatePartExpression_Executed(object sender, ExecutedRoutedEventArgs e) 
        { 
            ColumnSpecification columnSpecification = new ColumnSpecification(String.Empty, String.Empty, String.Empty); 
            NumberExpression numberExpression = new NumberExpression(columnSpecification, NumberComparisonOp.Equal, 0); 
            _observableInnerExpressions.Add(numberExpression); 
            listViewSimpleExpression.DataContext = _observableInnerExpressions
            e.Handled = true

        } 
end
There are two posibilities for this:

1. NumberExpression is an struct
2. NumberExpression has overriden GetHashcode and Equals

Then both objects are equal even if the reference is different

No comments:

Post a Comment