Below is a simple WPF application which displays Customers with the most orders which is collected from the database via LINQ-to-SQL in the code-behind.
What is the best way to extend this WPF application so that the user can select from a dropdown, e.g.:
- Customers with the most orders
- Customers with the least orders
- Customers by city
- Customers by state
and the appropriate collection will be shown?
Here are some ideas:
- have many dockpanels with all the data loaded which are turned Visible/Collapse by the event handler (quick and dirty approach)
- have a ContentControl which is populated via DelegateCommand in a ViewModel which somehow fills the ContentControl with a ViewModel which is then attached to a View via a Converter in the XAML
- use a MVP pattern and have the Presenter fill the ContentControl with the appropriate View (bound to the Presenter's collections), this would allow you to only load the data right before it would be viewed
- or is there a way to do this in simple code-behind but where the data would only be loaded when the user chooses the selection in the drop down
How do you solve this common line-of-business issue in your WPF/Silverlight apps, i.e. customer clicks on control and an area of the screen shows him appropriate information?
XAML:
<Window x:Class="TestLinqToSql123.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="500">
<Window.Resources>
<DataTemplate x:Key="CustomerTemplate">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}The customer {0} lives in {1} and has {2} orders.">
<Binding Path="Name"/>
<Binding Path="City"/>
<Binding Path="NumberOfOrders"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</Window.Resources>
<DockPanel>
<Border
Background="Orange"
CornerRadius="5"
Margin="10"
Padding="10">
<ScrollViewer>
<StackPanel>
<TextBlock Text="Customers with the most orders:"
Margin="0 0 0 10"
FontSize="18"/>
<ItemsControl x:Name="TheList"
ItemTemplate="{StaticResource CustomerTemplate}"/>
</StackPanel>
</ScrollViewer>
</Border>
</DockPanel>
</Window>
Code-Behind:
using System.Windows;
using TestLinqToSql123.Models;
using System.Linq;
namespace TestLinqToSql123
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
NorthwindDataContext db = new NorthwindDataContext();
var customers = from c in db.Customers
orderby c.Orders.Count descending
select new
{
Name = c.ContactName,
c.City,
NumberOfOrders = c.Orders.Count
};
TheList.ItemsSource = customers;
}
}
}