1

假设我有一个包含这两个不朽表的数据集:Employee & Order
Emp -> ID, Name
Ord -> Something, Anotherthing, EmpID
和关系Rel : Ord (EmpID) -> Emp (ID)

它在标准的主/细节场景中效果很好
(显示员工、跟踪关系、显示相关订单),
但是当我不想走相反的路(显示带有 Emp.Name 的 Ord 表)时怎么办?

像这样的东西:

<stackpanel>   // with datacontext set from code to dataset.tables["ord"]
   <TextBox Text="{Binding Something}"/>
   <TextBox Text="{Binding Anotherthing}"/>
   <TextBox Text="{Binding ???}"/> // that's my problem, how to show related Emp.Name 
</stackpanel>

有任何想法吗?我可以创建值转换器,但如果我不想使用从父模块获得的数据集实例,它就会变得棘手。

4

3 回答 3

1

如果要同步多个控件的内容,则需要通过在公共父控件上设置的DataContext让它们共享相同的绑定源。这是一个例子:

<StackPanel>
    <StackPanel.Resources>
        <ObjectDataProvider x:Key="ds" ObjectType="{x:Type mynamespace:MyDataSet}" />
    </StackPanel.Resources>

    <!-- We set the data context to the collection of rows in the table -->
    <StackPanel DataContext="{Binding Source={StaticResource ds}, Path=USERS.Rows}">
        <ListBox ItemsSource="{Binding}"
                 DisplayMemberPath="NAME"
                 IsSynchronizedWithCurrentItem="True" />
        <TextBox Text="{Binding Path=NAME}"/>
        <TextBox Text="{Binding Path=COUNTRIESRow.NAME}"/>
    </StackPanel>
</StackPanel>

IsSynchronizedWithCurrentItem属性设置为“True”将导致ListBox.SelectedItem属性与绑定源的CollectionView.CurrentItem自动同步,即在DataContext中设置的行集合。这意味着 ListBox 中当前选定的行成为两个 TextBox 控件的绑定源。

于 2008-10-24T08:56:43.573 回答
0

假设您使用的是强类型数据集,为了将 TextBox 绑定到“EmpRow.Name”属性,您可能必须将其作为“OrdDataTable”类的属性公开。

由于 Visual Studio 生成带有部分类的类型化 DataSet 代码,因此您可以通过以下方式将属性添加到“OrdDataTable”类:

using System.Data;

public partial class OrdDataTable : DataTable
{
    public string EmpName
    {
        get { return this.EmpRow.Name; }
    }
}

然后,您将能够绑定到数据上下文中的“OrdDataTable”对象的“EmpName”属性。

于 2008-10-16T08:43:12.823 回答
0

两个 TextBox 控件的 DataContext 是什么?
要使第二个绑定起作用,必须将DataContext设置为“USERSDataTable”的一个实例。由于这些都包含在 DataSet 中的数组中,因此您必须明确告诉您要绑定到哪个表。就像是:

<StackPanel>
    <StackPanel.Resources>
        <ObjectDataProvider x:Key="ds" ObjectType="{x:Type mynamespace:MyDataSet}" />
    </StackPanel.Resources>

    <!-- Notice we set the data context to the first item in the array of tables -->
    <StackPanel DataContext="{Binding Source={StaticResource ds}, Path=USERS[0]}">
        <TextBox Text="{Binding NAME}"/>
        <TextBox Text="{Binding COUNTRIESRow.NAME}"/>
    </StackPanel>
</StackPanel>
于 2008-10-21T09:01:27.947 回答