Reputation: 785
I am trying to create Data Form that has set of fields and paging buttons in the bottom of the form.
I would like the Paging to be a separate control with First, Previous, Next, Last with label Item 1 of n.
This paging control will be used any data entry form to allow the user to back and forth between the previous record. For example, Orders, Invoices, Payments are the data forms. The user when he selects Orders, New Order Form is shown. It also has the paging buttons to move to the previous record.
I created a UserControl named DataPager with dependency property of PagingItems. I want this Dependency Property to be generic so that I can pass a list of items (Order, Invoice, Payment)
For this I did: List in the user control. I tried binding this in the form that needs to page.
public List<object> Items
{
get { return (List<object>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
// Using a DependencyProperty as the backing store for Items. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(List<object>), typeof(DataPager), new UIPropertyMetadata(null, LoadItems));
private static void LoadItems(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
DataPager thisControl = (DataPager)obj;
thisControl.RefreshItems();
}
I am getting the following error in the page where I use the control and bind:
System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Collections.Generic.List`1[PagingSample.Order]' and 'System.Collections.Generic.List`1[System.Object]'. Consider using Converter property of Binding. BindingExpression:Path=Orders; DataItem='MainViewModel' (HashCode=26754911); target element is 'DataPager' (Name='dataPager1'); target property is 'Items' (type 'List`1')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='System.Collections.Generic.List`1[PagingSample.Order]' BindingExpression:Path=Orders; DataItem='MainViewModel' (HashCode=26754911); target element is 'DataPager' (Name='dataPager1'); target property is 'Items' (type 'List`1')
Not sure how I can keep the DataPager controls item property to be generic. I have not figured out yet how to tell the parent control the CurrentItem to show.
But wanted to clear the first hurdle. Any help appreciated.
Upvotes: 1
Views: 641
Reputation: 25201
You can't cast List<Order>
to List<object>
- that's why you're getting this error.
If your DataPager
control only needs to control which page is displayed, and not actually modify the collection, you could simply define your Items
property to be of type IEnumerable
instead of List<object>
, and that problem will be solved.
This is because for any T
, List<T>
is castable to IEnumerable
.
Upvotes: 1