I have an object, Customer, that has a column CustomerId. I would like to have a single choice action list that is populated with all the CustomerId's from the database.
What is the best way to populate a single choice action, dynamically, with the values from a particular column of an object?
Thanks,
Chris
How to populate single choice action's Items collection
Answers
Hello, Chris.
I believe the controller below does exactly what you want:
C#public partial class CustomersController : ViewController {
SingleChoiceAction customersAction;
const string refreshItemId = "refersh";
public CustomersController() {
InitializeComponent();
RegisterActions(components);
customersAction =
new SingleChoiceAction(this, "Customers", PredefinedCategory.RecordsNavigation);
customersAction.Caption = "Customers";
customersAction.ItemType = SingleChoiceActionItemType.ItemIsOperation;
customersAction.Execute += customersAction_Execute;
}
protected override void OnActivated() {
base.OnActivated();
PopulateItemsCollection();
}
void PopulateItemsCollection() {
customersAction.Items.Clear();
customersAction.Items.Add(new ChoiceActionItem(refreshItemId, "Refresh Customers", null));
List<SortProperty> sortProperties = new List<SortProperty>();
sortProperties.Add(new SortProperty("CustomerId", SortingDirection.Ascending));
foreach (Customer customer in
View.ObjectSpace.CreateCollection(typeof(Customer), null, sortProperties)) {
string itemCaption =
CaptionHelper.GetMemberCaption(
typeof(Customer), "CustomerId") + " :" + customer.CustomerId;
customersAction.Items.Add(
new ChoiceActionItem(customer.CustomerId, itemCaption, customer));
}
}
void customersAction_Execute(object sender, SingleChoiceActionExecuteEventArgs e) {
if (e.SelectedChoiceActionItem.Id == refreshItemId) {
PopulateItemsCollection();
}
else {
IObjectSpace objectSpace = Application.CreateObjectSpace();
Customer customer = objectSpace.GetObject(e.SelectedChoiceActionItem.Data as Customer);
e.ShowViewParameters.CreatedView = Application.CreateDetailView(objectSpace, customer);
}
}
}
The PopulateItemsCollection method adds all Customer objects to the action's Items collection. The supplementary "Refresh Customers" item is intended for manual refreshing. If it is required to filter the Customer objects displayed as the Action's items, pass the corresponding criteria as the second parameter of the ObjectSpace.CreateCollection method. If you need additional assistance, let me know.
Thanks,
Konstantin B
In this scenario, use PopupWindowChoiceAction instead of SingleChoiceAction. In the popup window, the Customer List View can be displayed. An example is provided in the Add an Action that Displays a Pop-up Window tutorial.