开发者

How to Add an element to the source of a PagedCollectionView

in my Silverlight-4-App, I have a PagedCollectionView. Eventually, I want to add a new item to it's so开发者_StackOverflow社区urce. Unfortunately, I find no example how to do this.

The AddNew() methode excepts no parameter.

The CurrentAddItem is readonly.

The SourceCollection is a IEnumerable<>.

I didn't think, that such a simple, standard task like adding an element to a collection could be so complicated. I didn't find any examples how to do this. Can anyone give me an example, how to add an element to a PagedCollectionView?

Thanks in advance,

Frank


MyObject newO = (MyObject)_PagedList.AddNew(); 
newO.SetProperty="Make the changes to the object";
_PagedList.CommitNew(); 


In addition to directly manipulating the PagedCollectionView, you can use MVVM to implement your functionality (this is a perfect example of how MVVM helps keep things simple by removing the need to dig into the innards of the UI).

PagedCollectionView is just that a View. There should be an ObservableCollection backing it. Adding a new item to that observable collection will add it to the view automatically. This works great with a ViewModel that might expose an observable collection say Orders

public class OrdersViewModel:INotifyPropertyChanged
{
  //Probably want to initialize this in a constructor to populate the collection
  private ObservableCollection<Order> _orders= new ObservableCollection<Order>();
  private ICommand _addOrder;

  public OrdersViewModel()
  {
    _addOrderCommand = new DelegateCommand(execute:(obj)=>AddOrder());
    //or use the relay command depending on which framework you're using
  }

  public ICommand AddOrderCommand{get {return _addOrder}};

  public ObservableCollection<Order> Orders
  {
    get{return _orders;}
    //I usually don't add a public setter for ObservableCollections
  }

  public void AddOrder()
  {
    //replace with your own logic of course
    _orders.Add(new Order());
  }
}

In your XAML you bind your PagedCollectionView to Orders and create a Button bound to AddOrderCommand. Your view should update with a new order.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜