Frage

I am relatively new to WPF was wondering if anyone could help me out.

I have a collectionview that holds an observablecollection list that stores all my items. My datagrid's item source is the collection view. I have simple grouping in datagrid XAML...

            <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}" FontWeight="Bold" Padding="3"/>
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </DataGrid.GroupStyle>

Now... adding / removing items works just fine, however whenever I update / edit an item the groups are not updated. Let's say for example the items are grouped by city, if I have 3 items under one city name "Seattle" then change one of those city names to "Brooklyn" the "Brooklyn" city remains under the Seattle group until i repopulate the observablecollection list.

My current work around... is that whenever a city is changed I am literally clearing and repopulating my observablecollectionlist for the groups to update... There has to be a better way!

My question is, is there any way to update the datagrid group, manually / dynamically whenever i update / edit an item?

Note INotifyPropertyChanged I am 90% sure is not the problem here, I have tried all manner of combinations with this interface, the groups will not update whenever I am editing / updating items. However whenever I add an item if it is a new city, a new group would be created, if I removed all items in a city, that city group would also be removed. Also its not just specific items, its any item column.

EDIT ok, still no solution... but I found another work around, which once again is not the most elegant. I put an event handler for the city box.. so whenever the city is changed the event handler is called, and I refresh the collectionview. However the event is called whenever I add an item, called when I click on the item, and called twice whenever I make a change so it's not every efficient.. still looking for ideas.

EDIT2 I am now looking into the BeginEdit and EndEdit of IEditableObject to see if this will give me what I need

Any help is GREATLY appreciated, thank you!

War es hilfreich?

Lösung

Ok well.... I could not find a direct way to update grouping as I updated my items. However I found a work around.. I implemented a event that is fired whenever I change text within the text box, inside the event and after some validation I call the refresh() method on the collectionview and the grouping updates.

This solution I kind of cringe over because calling refresh will call the textbox changed event for each textbox being refreshed, just numerous unnecessary calls to that event that goes through my validation within that event. So the more textboxes I have, the more times the event will be called whenever i refresh the view for a simple text box change.

If anyone else has any solutions or ideas please let me know as I am going to have to stick with this solution for now :(

EDIT Pseudo-code solution if anyone is curious...

When textbox gets focus, set a editing flag to be true, so no transactions on the textbox can happen during editing.

When textbox loses focus set editing flag to be false, check to make sure the current text is different from the previous text.

if the current text within the current textbox is different refresh the collectionview object

Andere Tipps

I ran into the same problem. Got it working with an ObservableCollectionImplementation, which yields item changes by a seperated Eventhandler (Look here). So within the ViewModel I just subscribe ItemPropertyChanges in an eventhandler and force everything to refresh.

Maybe it still helps someone.

Regards!

public class MyViewModel: ViewModelBase
{
  ObservableCollectionEx<IEditableItem> _rawsource;
  ICollectionView _viewSource;

  public MyViewModel (ObservableCollectionEx rawDataSource)
  {
    this._rawsource = rawDataSource;
    this._rawsource.ItemPropertyChanged += new PropertyChangedEventHandler(RawSource_ItemPropertyChanged);

    // Create the CollectionView
    this._viewSource = new ListCollectionView(this._rawsource);
  }

  void RawSource_ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    // Check if this is a Collection of EditableObjects - so a CommitEdit can be fired
if (this._viewSource is IEditableCollectionView)
    {
      var so = this._viewSource as IEditableCollectionView;
      so.CommitEdit();
    }

// CollectionView refresh
this._viewSource.Refresh();
  }

  // Here is our Bindable object
  public ICollectionView ItemSourcer
  {
get
{
  return this._viewSource;
}
  }

 // ...

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top