开发者

IEditableCollectionView loses selection on CommitEdit

开发者 https://www.devze.com 2023-04-08 09:46 出处:网络
I have CollectionViewSource in which dates grouped by years and months. Dates are displayed in TreeView (accurately in RadTreeView).

I have CollectionViewSource in which dates grouped by years and months. Dates are displayed in TreeView (accurately in RadTreeView).

The target is to change selected date without recreating the view (do not call Refresh method).

To do this I implemented IEditableObject in date view model and changed date so:

var selectedDate = SelectedDate;

var editableCollectionView = Dates.View as IEditableCollectionView;
if (null != editableCollectionView && !editableCollectionView.IsEditingItem)
{
    editableCollectionView.EditItem(selectedDate);
    selectedDate.Date = dt.Date;
    editableCollectionView.CommitEdit();
}

But in this case TreeView lost selection and I need to select "selected item" again that leads refreshing data bounded to selected date.

How can I solve this issue? Perfectly using MVVM way.

UPDATE:

If date is alone in group, changing date causes collapsing item which contains it.

UPDATE 2:

May be I shouldn't use SelectedDate property and work o开发者_运维问答nly with IsSelected and IsExpanded?


Leverage MVVM for Tree View Item.

Include two writeable properties in your item level class (which serves as the data context to your individual tree view item)

  1. IsExpanded
  2. IsSelected

Have INotifyPropertyChanged implemented and property changed notification raised in the setter of the above two properties.

Now at TreeViewLevel have a Style that binds these properties.

 <TreeView.Resources>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    ....
 </...> 

This way the expansion and selection is maintained on the tree view as long as it is maintained in the tree view item's data context.

Now remember that expanded states can be true for multiple items but selection state true is applicable for only one item along the entire tree view.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号