I have been working through a very small-scale WPF project in order to familiarize myself with it while I read Nathan’s book. I am attempting to do declarative binding on a single window with multiple tables from the same dataset. The schema (names have been changed to protect the innocent) is: tblMany2–tblOne–tblMany1
XAML is below, but in short:
- I set the datacontext in the windows _loaded handler. I have tried both to the dataset and to the table which is the primary table conceptually (tblMany1).
- I set the ItemSource on a combo box to tblMany1.
- I set the ItemSource on a second combo box to the foreign key data relation (originally it was a tbo, but I’ve been working for a while).
- The idea is to control the second combo box (and other controls) by changing the first.
- The result so far is a blank entry in the second combobox with a debug output saying cannot find the property of whichever object I’ve set the ItemsSource to.
XAML:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace;system;assembly=mscorlib"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:my="clr-namespace:MyProject"
xmlns:dx="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Height="500"
Width="700"
d:DesignHeight="350" d:DesignWidth="525" SizeToContent="WidthAndHeight">
<Window.Resources>
<!--Data-->
<!--Styles-->
<Style x:Key="buttonStyle">
<Setter Property="Button.Width" Value="85" />
<Setter Property="Button.Height" Value="30" />
</Style>
<Style x:Key="chkImageStyle" TargetType="Image">
<Setter Property="Image.Height" Value="25" />
<Setter Property="Image.Width" Value="30" />
<Setter Property="Image.Margin" Value="100,30,0,0" />
<Setter Property="Image.Stretch" Value="Fill" />
<Setter Property="Image.VerticalAlignment" Value="Top" />
<Setter Property="Grid.Column" Value="1" />
<Setter Property="Image.Source" Value="checkmark.jpg" />
<Setter Property="Image.Visibility" Value="hidden" />
</Style>
<!--Data Tempaltes-->
<DataTemplate x:Key="tblMany1Date">
<TextBlock Text="{Binding Path=tblMany1Date, StringFormat=d,dx:PresentationTraceSources.TraceLevel=High}" />
</DataTemplate>
<DataTemplate x:Key="tblOneLink">
<TextBlock HorizontalAlignment="Center">
<Hyperlink NavigateUri="{Binding Path=tblOne.Link}">
<Run Text="{Binding Path=tblOne.Name}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</Window.Resources>
<Viewbox Stretch="Uniform" Height="500" Width="750">
<!-- Main Dockpanel-->
<DockPanel Name="DockPanel1">
<!-- NavPane -->
<StackPanel Height="315" Background ="LightBlue" DockPanel.Dock="Left" Name="StackPanel1" Width="135">
<Button Margin="5" Content="New" Name="btnNewOne" Style="{StaticResource buttonStyle}"/>
<Label Margin="0" Content="ManyDate:" Name="lblDate" />
<!--Primary Control-->
<ComboBox Margin ="0" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=tblMany1}"
ItemTemplate="{StaticResource tblMany1Date}" Height="23" Name="cboDate" Width="120"
ForceCursor="False" AllowDrop="False" />
<TextBlock Margin="-5" Visibility="Hidden"/>
<Label Margin="0" Content="OneName:" Name="lblOneName" />
<ComboBox Margin="0" ItemsSource="{Binding FK_tblMany1_tblOne}"
ItemTemplate="{StaticResource tblOneLink}" Name="cboOne" />
</StackPanel>
</DockPanel>
</Viewbox>
As far as I see, you bind two comboboxes to the same data context.
But if I understand you correctly, you want the second combobox to display the items related with the selected item of the first combobox.
You can achieve this either in this way (change the datacontext):
Or in this way:
The slash sign in the code
tblMany1/FK_tblMany1_tblOneindicates that the binding takes the current item of thetblMany1collection and then takes the propertyFK_tblMany1_tblOneof that item.Edit
Because there is only two tables and you want to display the same collection (but with different fields), the correct code might look so: