0

我在 SketchFlow 中使用两个 DataGrid 创建了一个简单的主详细信息屏幕,但是当在主 DataGrid 中选择一行时,无法更新子 DataGrid。奇怪的是,如果孩子是 ListBox 而不是 DataGrid,它工作得非常好。

我使用 Blend 创建了一些分层样本数据,如下所示:

<xs:schema xmlns:tns="Expression.Blend.SampleData.SampleDataSource" xmlns:blend="http://schemas.microsoft.com/expression/blend/2008" targetNamespace="Expression.Blend.SampleData.SampleDataSource" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="SampleDataSource" type="tns:SampleDataSource" />
  <xs:complexType name="SampleDataSource">
    <xs:sequence>
      <xs:element name="Collection" type="tns:ItemCollection" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ItemCollection">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="Item" type="tns:Item" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="Item">
    <xs:sequence>
      <xs:element name="ChildRecords" type="tns:ChildRecords" />
    </xs:sequence>
    <xs:attribute name="Property1" type="xs:string" />
    <xs:attribute name="Property2" type="xs:boolean" />
  </xs:complexType>
  <xs:complexType name="ChildRecords">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="ChildRecordsItem" type="tns:ChildRecordsItem" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ChildRecordsItem">
    <xs:attribute name="Property1" type="xs:string" />
    <xs:attribute name="Property2" type="xs:string" />
    <xs:attribute name="Property3" type="xs:string" />
  </xs:complexType>
</xs:schema>

相关的 XAML 如下。没有代码被使用,因为我们试图让我们的设计师能够使用 Blend UI 来做到这一点。

<data:DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="21,48,0,106" Width="274"
 AutoGenerateColumns="False"
 ItemsSource="{Binding Collection}" IsReadOnly="True">
 ...
</data:DataGrid>
<data:DataGrid Margin="316,48,39,0" AutoGenerateColumns="False"
 ItemsSource="{Binding SelectedItem.ChildRecords, ElementName=dataGrid, Mode=OneWay}" Height="141" VerticalAlignment="Top" IsReadOnly="True">
 ...
</data:DataGrid>

我几乎已经确定这是 DataGrid 中的一个错误,因为我无法以其他方式解释它如何用于 ListBox。

4

1 回答 1

0

This will work if you databind the datacontext of the 2nd datagrid to the selecteditem of the first, and then bind the itemssource to the ChildRecords property:

<data:DataGrid ItemsSource="{Binding ChildRecords, Mode=OneWay}" 
    DataContext="{Binding SelectedItem, ElementName=dataGrid, Mode=OneWay}"/>

Alternatively, you might be able to use the built in detail view: http://silverlight.net/learn/videos/silverlight-videos/simple-masterdetails-with-datagrid/

于 2010-02-08T23:04:40.167 回答