Reputation:
A list of products is used through a WPF application. The list lstProducts is created in the business tier of the application. The list is fairly stable over time ... products only updated every 6 months.
How can that list be instantiated in C# such that it is available throughout the application?
My C# Class
creates a list
namespace BusinessObjects
{
public class Products
{
public class Product
{
public Int64 ProductId { get; set; }
public string FileAs { get; set; }
}
public List<Product> lstFileAs { get; set; }
public Products()
{
//populate lstFileAs
}
}
}
ComboBoxes on various forms are databound as follows
products = new Products());
cboProducts.DataContext = products;
cboProducts.ItemsSource = products.lstFileAs;
cboCustomer.DisplayMemberPath = "FileAs";
Let us please consider it as read that we all prefer to avoid global variables. However we are putting a new front end on an old and widely used application which does use global variables. The old application is written in VB6 and runs well in spite of using global variables. We are instructed to make the minimum changes to avoid unnecessarily introducing bugs.
Upvotes: 0
Views: 1333
Reputation: 11514
You can create an ObjectDataProvider and CollectionViewSource in App.xaml (for instance) and reference that in your project. I demonstrate here a possible implementation. This code assumes you create a GetProducts() method.
App.xaml:
<ObjectDataProvider x:Key="ProductsObjDataProvider"
ObjectType="{x:Type BusinessObjects:Products}"
MethodName="GetProducts">
</ObjectDataProvider>
<CollectionViewSource x:Key="ProductsView" Source="{Binding Source={StaticResource ProductsObjDataProvider}}"/>
To bind a combobox:
<ComboBox Name="cboProducts"
DisplayMemberPath="FileAs"
ItemsSource="{Binding Source={StaticResource ProductsView}}"
SelectedValue="{Binding Path=ProductID, Mode=TwoWay}"/>
Upvotes: 2