Silas Dor

Hi,

I would like to achieve the master Detail behaviour, like i found it in an application example from Microsoft during runtime in C#. How do i realize: <ListBox ItemsSource="{Binding Path=Divisions/Teams}"/>
<ListBox ItemsSource="{Binding Path=Divisions/Teams}"/>

Since i can not change the binding path of the DataContext bindingsource at the moment i have no idea how to do this.

Here is the Snippet i would like to realize in C# :

<!-- <Snippet1> -->
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlnsTongue Tiedrc="clr-namespaceTongue TiedDKSample"
Width="400" Height="180"
Title="Master-Detail Binding"
Background="Silver">
<Window.Resources>
<src:LeagueList x:Key="MyList" />
<Style TargetType="StackPanel">
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="Margin" Value="10,0,10,0"/>
</Style>
<Style TargetType="ListBox">
<Setter Property="Height" Value="100"/>
</Style>
<Style TargetType="Label">
<Setter Property="FontSize" Value="12"/>
</Style>
</Window.Resources>

<DockPanel DataContext="{Binding Source={StaticResource MyList}}">
<StackPanel>
<Label>My Soccer Leagues</Label>
<ListBox ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="true"/>
</StackPanel>

<StackPanel>
<Label Content="{Binding Path=Name}"/>
<ListBox ItemsSource="{Binding Path=Divisions}"
IsSynchronizedWithCurrentItem="true"/>
</StackPanel>

<StackPanel>
<Label Content="{Binding Path=Divisions/Name}"/>
<ListBox ItemsSource="{Binding Path=Divisions/Teams}"/>
</StackPanel>
</DockPanel>

</Window>

Thank you very much in advanve!

cheers

Silas



Re: Windows Presentation Foundation (WPF) Master Detail binding behaviour during runtime in C#

Silas Dor

This is what i tried so far:

GroupBox grpFahrer = new GroupBox();
Binding bindFahrerModell = new Binding();
bindFahrerModell.Source = FahrerModell();
bindFahrerModell.Mode = BindingMode.OneWay;
grpFahrer.DataContext = bindFahrerModell;

grpFahrer.Header = "Fahrer";
grpFahrer.Content = comboFahrerName;

ComboBox comboFahrerName = new ComboBox();
stackFahrer.Children.Add(comboFahrerName);
comboFahrerName.SetBinding(ComboBox.ItemsSourceProperty, "Fahrer.Name");

For some reasons i does not work. Any suggestions
FahrerModell is an Class which inherits from an ObservableCollection of Fahrer.

Thanks
Silas




Re: Windows Presentation Foundation (WPF) Master Detail binding behaviour during runtime in C#

Thomas Huber

Hi Silas,

the example you mean is located in%WindowsSDK_WPF_Samples%\ConnectedData\MasterDetail\CSharp\

I've written a Window in C# that does the same thing as the XAML-Page. Instead of the Constructor you should use the Loaded-Event to init the Window:

Code Snippet

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

namespace SDKSample

{

public class MyWindow : Window

{

public MyWindow()

{

LeagueList l = new LeagueList();

DockPanel dp = new DockPanel();

dp.DataContext = l;

StackPanel sp1 = new StackPanel();

Label lbl1 = new Label();

lbl1.Content = "My Soccer Leagues";

ListBox list1 = new ListBox();

list1.SetBinding(ListBox.ItemsSourceProperty, new Binding());

list1.IsSynchronizedWithCurrentItem = true;

sp1.Children.Add(lbl1);

sp1.Children.Add(list1);

StackPanel sp2 = new StackPanel();

Label lbl2 = new Label();

Binding b = new Binding();

b.Path = new PropertyPath("Name");

lbl2.SetBinding(Label.ContentProperty, b);

ListBox list2 = new ListBox();

list2.IsSynchronizedWithCurrentItem = true;

Binding bDiv = new Binding();

bDiv.Path = new PropertyPath("Divisions");

list2.SetBinding(ListBox.ItemsSourceProperty, bDiv);

sp2.Children.Add(lbl2);

sp2.Children.Add(list2);

StackPanel sp3 = new StackPanel();

Label lbl3 = new Label();

Binding bName = new Binding();

bName.Path = new PropertyPath("Divisions/Name");

lbl3.SetBinding(Label.ContentProperty, bName);

ListBox list3 = new ListBox();

list3.IsSynchronizedWithCurrentItem = true;

Binding bTeams = new Binding();

bTeams.Path = new PropertyPath("Divisions/Teams");

list3.SetBinding(ListBox.ItemsSourceProperty, bTeams);

sp3.Children.Add(lbl3);

sp3.Children.Add(list3);

dp.Children.Add(sp1);

dp.Children.Add(sp2);

dp.Children.Add(sp3);

this.Content = dp;

}

}

}

Does that help you






Re: Windows Presentation Foundation (WPF) Master Detail binding behaviour during runtime in C#

Silas Dor

Hi Thomas,

Thank you very much! It does help me. I am afraid though it won't work in my aplication this way.

Basicly i have a bunch of drivers and i would like to display all the driver names in a Combobox. When the user selects a name in that ComboBox, a bunch of strings (age, etc) which belong to that driver should be displayed in a ListBox.

When i assign the DataContext to the Parent Element of the Controls (ComboBox and ListBox) and the binding path "Name" to the ComboBox, instead of displaying me the names, every character from the first name will be assigned to a single entry in that ComboBox.

I am thinking now in updating the ListBox in the SelectionChanged from the ComboBox.

cheers

Silas





Re: Windows Presentation Foundation (WPF) Master Detail binding behaviour during runtime in C#

lee d

You can try doing something like this

odp1 - gets list of cities

odp2 - gets list of people in the selected city

Code Snippet

<ObjectDataProvider x:Key="odp1" ObjectType="{x:Type local:Cities}" MethodName="GetCities" />

<ObjectDataProvider ObjectType="{x:Type local:People}" MethodName="GetPeople" x:Key="odp2">

<ObjectDataProvider.MethodParameters>

<system:String>null</system:String>

</ObjectDataProvider.MethodParameters>

</ObjectDataProvider>

<ComboBox DisplayMemberPath="." SelectedIndex="0" SelectedValue="{Binding Mode=OneWayToSource, BindsDirectlyToSource=true, Source={StaticResource odp2}, Path=MethodParameters[0]}" ItemsSource="{Binding Source={StaticResource odp1}}"></ComboBox>

<ListBox DisplayMemberPath="." ItemsSource="{Binding Source={StaticResource odp2}}"></ListBox>






Re: Windows Presentation Foundation (WPF) Master Detail binding behaviour during runtime in C#

Silas Dor

I have tryed this way to bind to the SelectedItem from the ComboBox (comboBox.SelectedItem). It works for the first time, but then obviously the binding does not get any notification when the selected Item changes. I even tryed to bind to the binding from the ComboBox

Anyway, if i wont find a better solution i can still stick to the SelectionChangedEventHandler. Not a good practice cos i wanted to avoid Events but it works.

Thank you all for the help!

Silas





Re: Windows Presentation Foundation (WPF) Master Detail binding behaviour during runtime in C#

Silas Dor

How does odp2 know which city is selected





Re: Windows Presentation Foundation (WPF) Master Detail binding behaviour during runtime in C#

lee d

selectedvalue of combobox1 is bound to the methodparameter of odp2