2010-01-29 64 views
0

我正在使用RIA Services構建Silverlight應用程序。我想實現主/細節行爲。這裏的訣竅是「細節」網格不能直接用主網格的「SelectedItem」屬性填充,我必須做的是從主網格中獲取一個字段並將其用作參數DataService的。綁定將如何?Silverlight Databind到參數化DataService

這是參數化查詢代碼:

public Usuarios GetUserFromId(int id) 
    { 
     return this.ObjectContext.Usuarios.Where(u => u.ID == id).First(); 
    } 

而這些都是在XAML來源:

<riaControls:DomainDataSource x:Name="DomainDataSourceRaceTrackGetUsersWithRole" AutoLoad="True" QueryName="GetUsersWithRoleQuery" LoadSize="20"> 
      <riaControls:DomainDataSource.DomainContext> 
      <App:DomainServiceRaceTrack></App:DomainServiceRaceTrack> 
     </riaControls:DomainDataSource.DomainContext> 
     </riaControls:DomainDataSource> 
    <riaControls:DomainDataSource x:Name="DomainDataSourceRaceTrackGetUserById" AutoLoad="True" QueryName="GetUserFromId"> 
     <riaControls:DomainDataSource.DomainContext> 
      <App:DomainServiceRaceTrack></App:DomainServiceRaceTrack> 
     </riaControls:DomainDataSource.DomainContext> 
    </riaControls:DomainDataSource> 

以下是我綁定主電網:

<data:DataGrid x:Name="DataGridUsers" AutoGenerateColumns="False" ItemsSource="{Binding Data, ElementName=DomainDataSourceRaceTrackGetUsersWithRole}"> 

然後在SelectedItemChanged上我需要填充Detail網格,但是我使用的綁定不起作用:

<dataControls:DataForm x:Name="dataForm1" Height="393" Width="331" 
          VerticalAlignment="Top"  
          Header="User Details" 
          CurrentItem="{Binding DataGridUsers.SelectedItem.Id, ElementName=DomainDataSourceRaceTrackGetUserById}" 
          HorizontalAlignment="Left" > 
       <dataControls:DataForm.EditTemplate> 

任何人都知道我在做什麼錯了?我想使用XAML而不是代碼隱藏。

謝謝!

回答

1

好吧!您必須將參數'id'傳遞給'DomainDataSourceRaceTrackGetUserById',否則RIA服務不知道從哪裏獲得'id'值。

<riaControls:DomainDataSource x:Name="DomainDataSourceRaceTrackGetUserById" AutoLoad="True" QueryName="GetUserFromId"> 
     <riaControls:DomainDataSource.DomainContext> 
      <App:DomainServiceRaceTrack></App:DomainServiceRaceTrack> 
     </riaControls:DomainDataSource.DomainContext> 

<riaControls:DomainDataSource.QueryParameters> <riaData:ControlParameter 
        ParameterName="id" 
        ControlName="DataGridUsers" 
        PropertyName="SelectedItem.Id" 
        RefreshEventName="SelectedItemChanged" /> </riaControls:DomainDataSource.QueryParameters> 
    </riaControls:DomainDataSource> 

和綁定你的ItemsSource dataForm1爲會員 'DomainDataSourceRaceTrackGetUserById' 的 '數據'。測試它,讓我知道如果我失去了一些東西。

希望這會有所幫助!

+0

我得到一個錯誤:當AutLoad設置爲True和CanLoad設置爲False QueryParameters不能改變。有任何想法嗎?另外,在第一次加載時,我得到錯誤加載操作失敗,查詢'GetUserFromId'。序列不包含任何元素。 有什麼建議嗎? – brafales 2010-02-01 10:40:23

+0

我認爲你在我們的PDC版本中遇到了一些限制,造成了一些令人頭痛的問題。當一個現有的負載正在等待時,我們正在阻止負載,並且這種情況非常頻繁。 該行爲將在我們的下一個版本中得到解決,以便在調用新的加載時,掛起的加載將被取消。 與此同時,您可能必須關閉子數據DomainDataSource的自動加載,偵聽主網格上的SelectionChange事件,取消子數據上的任何掛起的加載並調用新的加載。 – 2010-02-03 07:47:27

+0

好的,我會試試看。 感謝這兩個答案! – brafales 2010-02-03 21:25:30

1

這裏是完整的XAML。它執行得非常快,所以請讓我知道它是否可以做得更好。 Click Here完整項目的源代碼

<UserControl xmlns:my1="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit" x:Class="Silverlight4LobHol.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="600" d:DesignWidth="800" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Ria" xmlns:my="clr-namespace:Silverlight4LobHol.Web" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:my2="clr-namespace:System.Windows.Data;assembly=System.Windows.Controls.Ria" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="300" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="50" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="50" /> 
     </Grid.RowDefinitions> 
     <riaControls:DomainDataSource AutoLoad="True" LoadedData="orderDomainDataSource_LoadedData" Name="orderDomainDataSource" QueryName="GetOrdersQuery"> 
       <riaControls:DomainDataSource.DomainContext> 
       <my:NorthWindDomainContext /> 
      </riaControls:DomainDataSource.DomainContext> 
     </riaControls:DomainDataSource> 
     <data:DataGrid AutoGenerateColumns="False" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding ElementName=orderDomainDataSource, Path=Data}" Name="orderDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected"> 
      <data:DataGrid.Columns> 
       <data:DataGridTextColumn x:Name="customerIdColumn" Binding="{Binding Path=CustomerID}" Header="CustomerID" Width="SizeToHeader" /> 
       <data:DataGridTextColumn x:Name="freightColumn" Binding="{Binding Path=Freight}" Header="Freight" Width="SizeToHeader" /> 
       <data:DataGridTemplateColumn x:Name="orderDateColumn" Header="Order Date" Width="SizeToHeader"> 
        <data:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <controls:DatePicker SelectedDate="{Binding Path=OrderDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" /> 
         </DataTemplate> 
        </data:DataGridTemplateColumn.CellTemplate> 
       </data:DataGridTemplateColumn> 
       <data:DataGridTextColumn x:Name="orderIDColumn" Binding="{Binding Path=OrderID}" Header="Order ID" Width="SizeToHeader" /> 
       <data:DataGridTemplateColumn x:Name="requiredDateColumn" Header="Required Date" Width="SizeToHeader"> 
        <data:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <controls:DatePicker SelectedDate="{Binding Path=RequiredDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" /> 
         </DataTemplate> 
        </data:DataGridTemplateColumn.CellTemplate> 
       </data:DataGridTemplateColumn> 
       <data:DataGridTextColumn x:Name="shipAddressColumn" Binding="{Binding Path=ShipAddress}" Header="Ship Address" Width="SizeToHeader" /> 
       <data:DataGridTextColumn x:Name="shipCityColumn" Binding="{Binding Path=ShipCity}" Header="Ship City" Width="SizeToHeader" /> 
       <data:DataGridTextColumn x:Name="shipCountryColumn" Binding="{Binding Path=ShipCountry}" Header="Ship Country" Width="SizeToHeader" /> 
       <data:DataGridTextColumn x:Name="shipNameColumn" Binding="{Binding Path=ShipName}" Header="Ship Name" Width="SizeToHeader" /> 
       <data:DataGridTemplateColumn x:Name="shippedDateColumn" Header="Shipped Date" Width="SizeToHeader"> 
        <data:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <controls:DatePicker SelectedDate="{Binding Path=ShippedDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" /> 
         </DataTemplate> 
        </data:DataGridTemplateColumn.CellTemplate> 
       </data:DataGridTemplateColumn> 
       <data:DataGridTextColumn x:Name="shipPostalCodeColumn" Binding="{Binding Path=ShipPostalCode}" Header="Ship Postal Code" Width="SizeToHeader" /> 
       <data:DataGridTextColumn x:Name="shipRegionColumn" Binding="{Binding Path=ShipRegion}" Header="Ship Region" Width="SizeToHeader" /> 
       <data:DataGridTextColumn x:Name="shipViaColumn" Binding="{Binding Path=ShipVia}" Header="Ship Via" Width="SizeToHeader" /> 
      </data:DataGrid.Columns> 
     </data:DataGrid> 
     <controlsToolkit:BusyIndicator Grid.Row="1" Grid.Column="0" IsBusy="{Binding ElementName=orderDomainDataSource,Path=IsBusy}"></controlsToolkit:BusyIndicator> 
     <riaControls:DomainDataSource AutoLoad="True" Height="0" LoadedData="customerDomainDataSource_LoadedData" Name="customerDomainDataSource" QueryName="GetCustomersByCustomerIdQuery" Width="0" Visibility="Visible"> 
      <riaControls:DomainDataSource.DomainContext> 
       <my:NorthWindDomainContext /> 
      </riaControls:DomainDataSource.DomainContext> 
      <riaControls:DomainDataSource.QueryParameters> 
       <riaControls:ControlParameter ControlName="orderDataGrid" ParameterName="customerId" PropertyName="SelectedItem.CustomerID" RefreshEventName="SelectionChanged" /> 
      </riaControls:DomainDataSource.QueryParameters> 
     </riaControls:DomainDataSource> 
       <my1:DataForm Grid.Row="1" Grid.Column="1" ItemsSource="{Binding ElementName=customerDomainDataSource, Path=Data}" HeaderVisibility="Collapsed"></my1:DataForm> 

    </Grid> 
</UserControl> 
+0

這是否解決了這個問題?或者仍然有一些問題? – funwithcoding 2010-02-02 14:39:59