Binding a datagrid details view to a selected item in a listbox

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Im new to WPF, and am working a problem out of a book. Basically the window has a listbox and a datagrid. The list box is bound to the stores table in the pubs database using an entity data model. The datagrid is bound to the sales table
using the same entity model, which shows the order number and order date of the selected store in the listbox.
The problem is that when I run the program, the listbox fills with the store names from the stores table, but the datagrid only shows the columns.
Here is the code:
Code:
<pre style="font-family:Consolas; background:white; color:black; font-size:13px <span style="color:blue <<span style="color:#a31515 Window<span style="color:red  x<span style="color:blue :<span style="color:red Class<span style="color:blue =<span style="color:blue "Act11_3.MainWindow"
       <span style="color:red  xmlns<span style="color:blue =<span style="color:blue "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       <span style="color:red  xmlns<span style="color:blue :<span style="color:red x<span style="color:blue =<span style="color:blue "http://schemas.microsoft.com/winfx/2006/xaml"
       <span style="color:red  Title<span style="color:blue =<span style="color:blue "MainWindow"<span style="color:red  Loaded<span style="color:blue =<span style="color:blue "Window_Loaded"<span style="color:red  Width<span style="color:blue =<span style="color:blue "375"<span style="color:red  Height<span style="color:blue =<span style="color:blue "300"<span style="color:blue >
<span style="color:#a31515     <span style="color:blue <<span style="color:#a31515 Grid<span style="color:blue >
<span style="color:#a31515         <span style="color:blue <<span style="color:#a31515 DockPanel<span style="color:blue >
<span style="color:#a31515             <span style="color:blue <<span style="color:#a31515 ListBox<span style="color:red  Name<span style="color:blue =<span style="color:blue "StoresList"<span style="color:red  DockPanel.Dock<span style="color:blue =<span style="color:blue "Left"<span style="color:red  ItemsSource<span style="color:blue ="{<span style="color:#a31515 Binding<span style="color:blue }<span style="color:blue " 
                    <span style="color:red  Height<span style="color:blue =<span style="color:blue "330"<span style="color:red  Width<span style="color:blue =<span style="color:blue "198"<span style="color:red  IsSynchronizedWithCurrentItem<span style="color:blue =<span style="color:blue "True"<span style="color:blue  >
<span style="color:#a31515                 <span style="color:blue <<span style="color:#a31515 ListBox.ItemTemplate<span style="color:blue >
<span style="color:#a31515                     <span style="color:blue <<span style="color:#a31515 DataTemplate<span style="color:blue >
<span style="color:#a31515                         <span style="color:blue <<span style="color:#a31515 TextBlock<span style="color:red  FontWeight<span style="color:blue =<span style="color:blue "Bold"<span style="color:red  Text<span style="color:blue ="{<span style="color:#a31515 Binding<span style="color:red  Path<span style="color:blue =<span style="color:blue stor_name<span style="color:blue }<span style="color:blue "<span style="color:blue  />
<span style="color:#a31515                     <span style="color:blue </<span style="color:#a31515 DataTemplate<span style="color:blue >
<span style="color:#a31515                 <span style="color:blue </<span style="color:#a31515 ListBox.ItemTemplate<span style="color:blue >
<span style="color:#a31515             <span style="color:blue </<span style="color:#a31515 ListBox<span style="color:blue >
<span style="color:#a31515             <span style="color:blue <<span style="color:#a31515 DataGrid<span style="color:red  Name<span style="color:blue =<span style="color:blue "SalesGrid"<span style="color:red  DockPanel.Dock<span style="color:blue =<span style="color:blue "Right"
                     <span style="color:red  ItemsSource<span style="color:blue ="{<span style="color:#a31515 Binding<span style="color:red  Path<span style="color:blue =<span style="color:blue sales<span style="color:blue }<span style="color:blue "<span style="color:red  AutoGenerateColumns<span style="color:blue =<span style="color:blue "False"<span style="color:red  Width<span style="color:blue =<span style="color:blue "209"<span style="color:blue  >
<span style="color:#a31515                 <span style="color:blue <<span style="color:#a31515 DataGrid.Columns<span style="color:blue >
<span style="color:#a31515                     <span style="color:blue <<span style="color:#a31515 DataGridTextColumn<span style="color:red  Header<span style="color:blue =<span style="color:blue "Order Number"<span style="color:red  Binding<span style="color:blue ="{<span style="color:#a31515 Binding
                       <span style="color:red  Path<span style="color:blue =<span style="color:blue ord_num<span style="color:blue }<span style="color:blue "<span style="color:blue  />
<span style="color:#a31515                     <span style="color:blue <<span style="color:#a31515 DataGridTextColumn<span style="color:red  Header<span style="color:blue =<span style="color:blue "Order Date"<span style="color:red  Binding<span style="color:blue ="{<span style="color:#a31515 Binding 
                       <span style="color:red  Path<span style="color:blue =<span style="color:blue ord_date<span style="color:blue }<span style="color:blue "<span style="color:blue  />
<span style="color:#a31515                 <span style="color:blue </<span style="color:#a31515 DataGrid.Columns<span style="color:blue >
<span style="color:#a31515             <span style="color:blue </<span style="color:#a31515 DataGrid<span style="color:blue >
<span style="color:#a31515         <span style="color:blue </<span style="color:#a31515 DockPanel<span style="color:blue >
<span style="color:#a31515     <span style="color:blue </<span style="color:#a31515 Grid<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Window<span style="color:blue >

<pre style="font-family:Consolas; background:white; color:black; font-size:13px <span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.Windows;
<span style="color:blue using System.Windows.Controls;
<span style="color:blue using System.Windows.Data;
<span style="color:blue using System.Windows.Documents;
<span style="color:blue using System.Windows.Input;
<span style="color:blue using System.Windows.Media;
<span style="color:blue using System.Windows.Media.Imaging;
<span style="color:blue using System.Windows.Navigation;
<span style="color:blue using System.Windows.Shapes;

<span style="color:blue namespace Act11_3
{
<span style="color:gray ///<span style="color:green <span style="color:gray <summary>
<span style="color:gray ///<span style="color:green Interaction logic for MainWindow.xaml
<span style="color:gray ///<span style="color:green <span style="color:gray </summary>
<span style="color:blue public <span style="color:blue partial <span style="color:blue class <span style="color:#2b91af MainWindow : <span style="color:#2b91af Window
{
<span style="color:blue public MainWindow()
{
InitializeComponent();
}

<span style="color:blue private <span style="color:blue void Window_Loaded(<span style="color:blue object sender, <span style="color:#2b91af RoutedEventArgs e)
{
<span style="color:#2b91af pubsEntities db = <span style="color:blue new <span style="color:#2b91af pubsEntities();
<span style="color:blue this.StoresList.DataContext = db.stores;
}

<span style="color:blue private <span style="color:blue void StoresList_Click(<span style="color:blue object sender, <span style="color:#2b91af KeyEventArgs e)
{
SalesGrid.SelectedValuePath = StoresList.Items.CurrentItem.ToString();
SalesGrid.SelectedValue = StoresList.SelectedItem.ToString();
}
}
}
[/code]
[/code]
Any help will be greatly appreciated.

Tom
<hr class="sig Thomas G Magaro

View the full article
 
Back
Top