【发布时间】:2011-10-15 15:07:08
【问题描述】:
我一直在研究一个非常小规模的 WPF 项目,以便在阅读 Nathan 的书时熟悉它。我正在尝试在具有来自同一数据集的多个表的单个窗口上进行声明性绑定。架构(名称已更改以保护无辜者)为:tblMany2--tblOne--tblMany1
XAML 在下面,但简而言之:
- 我在 windows _loaded 处理程序中设置了数据上下文。我已经尝试过数据集和概念上作为主表的表 (tblMany1)。
- 我将组合框上的 ItemSource 设置为 tblMany1。
- 我将第二个组合框上的 ItemSource 设置为外键数据关系(最初它是一个 tbo,但我已经工作了一段时间)。
- 这个想法是通过更改第一个组合框来控制第二个组合框(和其他控件)。
- 目前的结果是第二个组合框中的空白条目,调试输出显示找不到我将 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>
【问题讨论】:
标签: .net wpf xaml data-binding