usguy

Hi,

I am new to this so please go slow with me. Smile I am trying to create master-detail relationship using two list boxes. I am using "Three Level Master Detail ADO" sample code as reference. I cant figure out what am i doing wrong. Please help. I am copy pasting code here. (By the way i have renamed fields CustomerID to code & CompanyName to name in customer table)

How I can achieve show orders only for selected customer in first listbox

Thank you

Window2.xaml

<Window x:Class="ATTest.Window2"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Test1" Height="277" Width="356" WindowStartupLocation="CenterScreen">

<Window.Resources>

<Style TargetType="{x:Type ListBox}">

<Setter Property="Height" Value="60"/>

<Setter Property="Width" Value="160"/>

</Style>

<Style TargetType="{x:Type Label}">

<Setter Property="FontWeight" Value="Bold" />

<Setter Property="Foreground" Value="Red" />

<Setter Property="VerticalAlignment" Value="Center" />

</Style>

<Style TargetType="{x:Type TextBlock}">

<Setter Property="VerticalAlignment" Value="Center" />

</Style>

</Window.Resources>

<Border Margin="30" BorderBrush="Blue" BorderThickness="2" Padding="10">

<StackPanel>

<ListBox Width="200" Margin="10" ItemsSource="{Binding Path=Customers}" Name="lstCustomer" IsSynchronizedWithCurrentItem="true" >

<ListBox.ItemTemplate>

<DataTemplate>

<StackPanel>

<StackPanel Orientation="Horizontal">

<Label VerticalAlignment="Center">Code:</Label>

<TextBlock Text="{Binding Path=code}" VerticalAlignment="Center"/>

</StackPanel>

<StackPanel Orientation="Horizontal">

<Label Foreground="Green" VerticalAlignment="Center">Name:</Label>

<TextBlock Text="{Binding Path=name}" VerticalAlignment="Center"/>

</StackPanel>

</StackPanel>

</DataTemplate>

</ListBox.ItemTemplate>

</ListBox>

<StackPanel Name="sp">

<StackPanel Orientation="Horizontal">

<StackPanel Margin="5">

<Label>Mountains</Label>

<ListBox ItemsSource="{Binding Path=Orders}" Name="lbDetails">

<ListBox.ItemTemplate>

<DataTemplate>

<StackPanel>

<StackPanel Orientation="Horizontal">

<Label VerticalAlignment="Center">OrderID:</Label>

<TextBlock Text="{Binding Path=OrderID}" VerticalAlignment="Center"/>

</StackPanel>

<StackPanel Orientation="Horizontal">

<Label Foreground="Green" VerticalAlignment="Center">CustID:</Label>

<TextBlock Text="{Binding Path=CustomerID}" VerticalAlignment="Center"/>

</StackPanel>

</StackPanel>

</DataTemplate>

</ListBox.ItemTemplate>

</ListBox>

</StackPanel>

</StackPanel>

</StackPanel>

</StackPanel>

</Border>

</Window>

Window2.xaml.cs

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Data.OleDb;

using System.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

namespace ATTest

{

/// <summary>

/// Interaction logic for Window2.xaml

/// </summary>

public partial class Window2 : Window

{

public OleDbConnection oleCon;

public OleDbCommand oleComd;

string strSql = "SELECT * FROM Customers";

public Window2()

{

InitializeComponent();

BindData();

}

public void BindData()

{

string mdbFile = System.IO.Directory.GetCurrentDirectory() + "\\NWind.mdb";

mdbFile = mdbFile.Replace("bin\\Debug\\", "");

oleCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbFile);

DataSet dtst = new DataSet();

OleDbDataAdapter adpt = new OleDbDataAdapter();

adpt.SelectCommand = new OleDbCommand("SELECT * FROM Customers;", oleCon) ;

adpt.Fill(dtst, "Customers");

lstEmployee.DataContext = dtst;

OleDbDataAdapter adpt2 = new OleDbDataAdapter();

adpt2.SelectCommand = new OleDbCommand("SELECT * FROM Orders;", oleCon);

adpt2.Fill(dtst, "Orders");

DataTableCollection tables = dtst.Tables;

dtst.Relations.Add("CustmerOrder",

tables["Customers"].Columns["code"],

tables["Orders"].Columns["CustomerID"]);

sp.DataContext = dtst.Tables["Orders"];

}

}

}



Re: Windows Presentation Foundation (WPF) Master detail..

Marlon Grech


you need to specify the DataContext for the firsst listbox as well...

ie.in the BindData method put the following code

lstCustomer.DataContext = your table...


Regards





Re: Windows Presentation Foundation (WPF) Master detail..

usguy

Hi,

Thank you for your reply. I already defined it xaml file but still as per your suggestion I added following code

DataTableCollection tables = dtst.Tables;

dtst.Relations.Add("CustmerOrder",

tables["Customers"].Columns["code"],

tables["Orders"].Columns["CustomerID"]);

sp.DataContext = dtst.Tables["Orders"];

lbDetails.DataContext = dtst.Tables["Orders"];

but it still doesnt work, nothing is displayed in listbox. But if i change xmal file to

</ListBox>

<StackPanel Name="sp">

<StackPanel Orientation="Horizontal">

<StackPanel Margin="5">

<Label>Mountains</Label>

<ListBox ItemsSource="{Binding}" Name="lbDetails"> // Removing Path=Orders

I can see records in listbox but if I change selection in 1st listbox , content of 2nd listbox doesnt change..any idea why Do I need to bind in xaml as well as in code behind too





Re: Windows Presentation Foundation (WPF) Master detail..

Marlon Grech


hello there,

that is totally expected...

Since you set the sourc to be the orders data table you should not put Path=Orders

So is this issue solved or is there anything else you would like to ask





Re: Windows Presentation Foundation (WPF) Master detail..

usguy

Oh Ok..thank you.

Issue is content in 2nd listbox (detail) doesnot change if you according to value selected in 1st listbox.





Re: Windows Presentation Foundation (WPF) Master detail..

Marlon Grech


ok now I am lost...

You have two listboxes and they are binding to different sources... they are not linked together, so the value of the 2nd listbox will not change when the selection of the 1st listox changes...

what exactly do you want, do you want to bind to the selected item of the listbox 1





Re: Windows Presentation Foundation (WPF) Master detail..

usguy

I apologize for confusion. Yes first listbox is for customers & 2nd list box is for orders. I want to bind orders list box to selected item in customers listbox. I think this is achieved by using relation object.

I am refering to sample http://www.beacosta.com/blog/ p=21






Re: Windows Presentation Foundation (WPF) Master detail..

Marlon Grech

ok... try this

DataTableCollection tables = dtst.Tables;

dtst.Relations.Add("CustmerOrder",

tables["Customers"].Columns["code"],

tables["Orders"].Columns["CustomerID"]);

sp.DataContext = dtst.Tables["Orders"];

lbDetails.DataContext = dtst.Tables["Orders"];

IBindingListCollectionView view = (BindingListCollectionView)CollectionViewSource.GetDefaultView(dtst.Tables["Orders"]);

view.CustomFilter = ¡°CustomerCode == ¡®"+((DataRow)otherListBox.SelectedItem)["code"].ToString()+"¡¯¡±;


hope it helps







Re: Windows Presentation Foundation (WPF) Master detail..

usguy

Thank you..I will try it. Appriciate your help.