2011-10-15 34 views
0

我一直在通過一個非常小規模的WPF項目,以便在閱讀Nathan的書時熟悉它。我試圖在同一個數據集中有多個表的單個窗口上進行聲明性綁定。架構(名稱已更改爲保護無辜者)是:tblMany2 - tblOne - tblMany1相關表wpf/xaml中的聲明性數據綁定

XAML低於,但在短:

  • 我設置_loaded處理程序窗口的datacontext。我已經嘗試了數據集和表概念上的主表(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> 

回答

0

據我看到的,你結合兩個組合框相同的數據上下文。 但是,如果我理解正確,您希望第二個組合框顯示與第一個組合框的選定項相關的項目。

<ComboBox DataContext="{Binding SelectedItem, ElementName=cboDate}" Margin="0" ItemsSource="{Binding FK_tblMany1_tblOne}" 
      ItemTemplate="{StaticResource tblOneLink}" Name="cboOne" /> 

或者以這樣的方式:

您可以用這種方式(改變的DataContext)實現這一

<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" /> 
+0

謝謝 - 這有助於我與之合作的關係之一。但在這個特定的一個,我仍然有問題。無論我做什麼,它都不承認這種關係中的外鍵。我有一種感覺,這與關係的「方向性」有關 - 我所綁定的選定項目是關係的許多方面,而第二個組合框的目標屬性是一方。這有意義嗎? – dansan

+0

@dansan您可以添加表格圖表的截圖到問題中嗎?瞭解該屬性是集合還是單個對象很重要。在這種情況下,我需要知道FK_tblMany1_tblOne和tblOne屬性 – vorrtex

+0

的類型,不幸的是我無法發佈圖像,因爲我非常失聲。但是,tblMany1通過tblOne_name varchar(50)字段與tblOne相關。外鍵是tblMany1_tblOne_Name。該關係被命名爲FK_Many1_tblOne。不幸的是,在數據集設計器中,您看不到多個表中關係的屬性,或者僅在一個表中。這就是爲什麼我懷疑作爲問題,因爲我試圖通過數據綁定從多人導航到一個。 – dansan

0

的東西,我不知道:數據集設計器中父子設置的重要性。我認爲通過顛倒它們,我也會顛倒這種關係。但是,情況並非如此。一直以來的問題是,tblMany1和tblOne之間的導航/關係屬性默認與概念情境相反。

感謝vorrtex關於「/」語法的重要信息。

相關問題