为了在阅读Nathan的书时熟悉它,我一直在完成一个非常小的WPF项目。我试图做声明性绑定与来自同一数据集的多个表的单个窗口。模式(已更改名称以保护无辜)为:tblMany2——tblOne——tblMany1
XAML在下面,但简而言之:
- 我在windows_loaded处理程序中设置了数据上下文。我已经尝试了数据集和表,这是概念上的主表(tblMany1)。
- 我将组合框的ItemSource设置为tblMany1。
- 我将第二个组合框上的ItemSource设置为外键数据关系(最初是两个,但我已经工作了一段时间)。 这个想法是通过改变第一个组合框来控制第二个组合框(和其他控件)。到目前为止,结果是在第二个组合框中出现一个空白条目,并且调试输出说无法找到我设置ItemsSource为的对象的属性。
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>
据我所知,您将两个组合框绑定到相同的数据上下文。但是,如果我理解正确的话,您应该希望第二个组合框显示与第一个组合框中所选项目相关的项目。
您可以通过以下方式实现(更改数据上下文):
<ComboBox DataContext="{Binding SelectedItem, ElementName=cboDate}" Margin="0" ItemsSource="{Binding FK_tblMany1_tblOne}"
ItemTemplate="{StaticResource tblOneLink}" Name="cboOne" />
或者这样:
<ComboBox Margin="0" ItemsSource="{Binding tblMany1/FK_tblMany1_tblOne}"
ItemTemplate="{StaticResource tblOneLink}" Name="cboOne" />
代码tblMany1/FK_tblMany1_tblOne
中的斜杠符号表示绑定获取tblMany1
集合的当前项,然后获取该项的属性FK_tblMany1_tblOne
。
编辑因为只有两个表,而您希望显示相同的集合(但使用不同的字段),所以正确的代码应该是这样的:
<ComboBox Margin="0" ItemsSource="{Binding tblMany1}"
ItemTemplate="{StaticResource tblOneLink}" Name="cboOne" />
一些我不知道的事情:父子设置在数据集设计器中的重要性。我假设通过对它们取反,我也可以将关系取反。然而,事实并非如此。一直以来的问题是tblMany1和tblOne之间的navigation/relationship属性是默认定义的,与概念情况相反。
感谢vorrtex提供的关于"/"语法的重要信息。