Reputation: 1270
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
Upvotes: 0
Views: 3675
Reputation: 19895
For your query... did you explore SynchronizeWithCurrentItem functionality in WPF?
Upvotes: 0
Reputation: 12786
No, I would indeed use CurrentItem of ICollectionView and in your XAML use IsSynchronizedWithCurrentItem
Upvotes: 2