0

I am trying to implement custom ObservableCollection Which will have current (selected) item property which can be directly bound from XAML This is sample code I got so far Could someone point me to the right direction? the idea here is to set listviews' selected item property directly to its itemsources' Currentitem and provide Action which will take argument as current item. this action will be set from viewmodel.

public class ItemAwareObservableCollection<T> : ObservableCollection<T>
{
    private readonly Action<T> _selectionCallback;
    private T _currentItem;

    public T CurrentItem
    {
        get { return _currentItem; }
        set
        {
            if(_currentItem.Equals(value))
            _currentItem = value;
            OnPropertyChanged(new PropertyChangedEventArgs("CurrentItem"));
            _selectionCallback(value);
        }
    }

    public ItemAwareObservableCollection(Action<T> selectionCallback)
    {
        _selectionCallback = selectionCallback;
    }

    public ItemAwareObservableCollection(Action<T> selectionCallback, IEnumerable<T> collection)
        : base(collection) { _selectionCallback = selectionCallback; }

    public ItemAwareObservableCollection(Action<T> selecytionCallback, List<T> list)
        : base(list) { _selectionCallback = selecytionCallback; }
}

and this is sample usage from viewmodel

get { return new ItemAwareObservableCollection<Companies>(onSelecttionchange, Resolve<ICompanyService>().Companies); }

inside XAML View I would like to bind this collection to the ItemSource of Llistview (this works perfectly), but I would like to bind its selecteditem property to CurrentItem of this collectiion

Rati_Ge
  • 1,262
  • 2
  • 15
  • 37
  • what do you mean? there is no similar question all samples show how to use separate property which we are using at the moment, but we want to go with this approach so could u provide us with helpful info? – Rati_Ge Jul 23 '12 at 10:16
  • He means your other questions. You've only accepted 4 out of 14 questions you've raised. (People appreciate upvotes & accepts when they spend the time to help.) – Steve Py Jul 23 '12 at 10:20

2 Answers2

2

No, I would indeed use CurrentItem of ICollectionView and in your XAML use IsSynchronizedWithCurrentItem

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
0

For your query... did you explore SynchronizeWithCurrentItem functionality in WPF?

Community
  • 1
  • 1
WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • 1
    And how about you make comments in the comment section, not in your answer? Just saying. http://meta.stackexchange.com/q/17447 – SynerCoder Jul 23 '12 at 10:21
  • I See your point, but if u examine my questions, u would see that in reality most of them were not answered and that's why I did not mark them as answered. I am not asking for exact code here All I want to know is if this approach is correct to implement Current Item in this way? – Rati_Ge Jul 23 '12 at 10:24
  • No, I would indeed use [CurrentItem](http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.currentitem.aspx) of [ICollectionView](http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx) and in your XAML use [IsSynchronizedWithCurrentItem](http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.issynchronizedwithcurrentitem.aspx) – SynerCoder Jul 23 '12 at 10:31
  • well that's what I call answer despite the fact we have to test this in out environment I will mark this as answer so pleas instead of using comment post this as answer so I can mark it – Rati_Ge Jul 23 '12 at 10:42
  • @Rati_Ge Well, that was akward :p 17sec before my answer you accepted the thsi one XD – SynerCoder Jul 23 '12 at 11:00