我想实现自定义ObservableCollection哪些将有当前(选定)项目属性,可以直接绑定从XAML这是我到目前为止的示例代码可能有人指向我正确的方向?这里的想法是将listviews的所选项目属性直接设置为其itemsources的Currentitem,并提供Action,它将参数作为当前项目。这个动作将从viewmodel设置。自定义ObservableCollection与选定的项目
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; }
}
,这是从视图模型样本使用
get { return new ItemAwareObservableCollection<Companies>(onSelecttionchange, Resolve<ICompanyService>().Companies); }
内XAML浏览我想这个集合绑定到Llistview的的ItemSource(这完美的作品),但我想它的selectedItem属性绑定property to CurrentItem of this collectiion
你是什么意思?没有类似的问题,所有样本都显示了如何使用目前我们正在使用的单独属性,但是我们希望采用这种方法,那么您是否可以向我们提供有用的信息? – 2012-07-23 10:16:57
他意味着你的其他问题。你只接受了你提出的14个问题中的4个。 (当人们花时间帮助时,人们会赞赏并赞同) – 2012-07-23 10:20:01