Silverlight WCF数据绑定相关表

时间:2013-04-13 21:10:18

标签: silverlight silverlight-4.0 silverlight-5.0

我有一张带外键的表。我想显示数据作为描述,而不是相关表的Id。在Entity服务中,我在数据模型上创建了.Include语句,以便返回。这是我试过的,该站出现在单元格中,但组合框中没有任何内容:

<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Width="Auto" Header="Station">
      <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Station.Description}" />
        </DataTemplate>
      </sdk:DataGridTemplateColumn.CellTemplate>
      <sdk:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
          <ComboBox ItemsSource="{Binding Station}" />
        </DataTemplate>
      </sdk:DataGridTemplateColumn.CellEditingTemplate>
    </sdk:DataGridTemplateColumn>

 <OperationContract()>
  Public Function LoadPersonnel(clientId As Integer) As List(Of Personnel)
    Dim result As List(Of Personnel)
    Try
      result = db.Personnels.Include("Station").Where(Function(o) o.ClientId = clientId).ToList
    Catch
      result = Nothing
    End Try
    Return result
  End Function

也尝试过:

<ComboBox ItemsSource="{Binding}" DisplayMemberPath="{Binding Station.Description}" SelectedItem="{Binding Station.Description}" SelectedValue="{Binding Station.StationId}" />

1 个答案:

答案 0 :(得分:1)

这样做,

  <sdk:DataGrid AutoGenerateColumns="False" Height="100" HorizontalAlignment="Left" Margin="107,90,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="251" >
            <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Width="Auto" Header="Station">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Station.Description}" />
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
                <sdk:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Station}" DropDownOpened="ComboBox_DropDownOpened" />
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellEditingTemplate>
            </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

在下拉打开事件中,分配itemssource,

 private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            var combo = (ComboBox)sender;
            combo.ItemsSource = Station;
            combo.DisplayMemberPath = "Description";
        }