我正在使用RIA Services构建Silverlight应用程序。我想实现主/细节行为。这里的技巧是“细节”网格不能直接用主网格的“SelectedItem”属性填充,我要做的是从主网格中获取一个字段并将其用作参数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而不是代码隐藏。
谢谢!
答案 0 :(得分: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>
并将您的dataForm1 ItemsSource绑定到'DomainDataSourceRaceTrackGetUserById'的成员'Data'。测试它,让我知道如果我遗失了什么。
希望这有帮助!
答案 1 :(得分: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>