2010-04-08 82 views
4

我有以下表格:C#WPF XAML绑定到数据表

公司{CompanyID,公司名称}
新政{CompanyID,值}

而且我有一个列表框:

<ListBox Name="Deals" 
     Height="100" Width="420" Margin="0,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" 
     Visibility="Visible" IsSynchronizedWithCurrentItem="True" 
     ItemsSource="{Binding}" SelectionChanged="Deals_SelectionChanged"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding companyRowByBuyFromCompanyFK.CompanyName}" FontWeight="Bold" /> 
       <TextBlock Text=" -> TGS -> " /> 
       <TextBlock Text="{Binding BuyFrom}" FontWeight="Bold" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

正如你所看到的,我想显示CompanyName而不是外键的ID。关系“companyRowByBuyFromCompanyFK”存在,就像在Deals_SelectionChanged中我可以访问Deals行的companyRowByBuyFromCompanyFK属性,我也可以访问该行的CompanyName属性。

是因为XAML绑定使用[]索引器而无法工作的原因?而不是DataTable中CompanyRows的属性?

目前即时得到的值,如

  • - > TGS - > 3
  • - > TGS - > 4

编辑绑定错误更新

System.Windows.Data Error: 39 : BindingExpression path error: 'companyRowByBuyFromCompanyFK' property not found on 'object' ''DataRowView' (HashCode=30295189)'. BindingExpression:Path=companyRowByBuyFromCompanyFK.CompanyName; DataItem='DataRowView' (HashCode=30295189); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

看起来像它没有将DataRowView项目转换为SuppliersRo w

完成此操作的最佳方法是什么?

  1. 制作一个转换器,使用被引用为自定义参数的表转换外键。

  2. 为每个表创建表视图?由于我有相当多的有外键的表,所以这会很长时间。

+1

如果添加一丝你的绑定: {绑定路径= companyRowByBuyFromCompanyFK.CompanyName,诊断:PresentationTraceSources.TraceLevel =高} 当列表框结合看你的输出窗口,它说什么了?这可能会让我们更深入地了解绑定中的失败。 – 2010-04-14 16:34:08

+1

Omg谢谢你!我总是忘记输出窗口! Wwhy不会显示在错误窗口!我现在修复它:)谢谢你的提示! – 2010-04-14 22:48:08

回答

2

有人杀我....谢谢你的输出窗口,我总是忘了有关指出! :(

我发现通过查看输出窗口中的溶液,其是以下

System.Windows.Data Error: 39 : BindingExpression path error: 'companyRowByBuyFromCompanyFK' property not found on 'object' ''DataRowView

我想出的是,结合的DataTemplate被结合的DataTemplate的的SelectedItem其是DataRowView的,而不是实际强类型数据幸运的是,DataRowView类有一个指向DataRow(这是supplierRow)类的Row属性,因此我可以像往常那样绑定这些数据,Fix在跟踪行中。

<TextBlock Text="{Binding companyRowByBuyFromCompanyFK.CompanyName}" FontWeight="Bold" /> 

此更改为下面的工作,因为我预料到。

<TextBlock Text="{Binding Row.companyRowByBuyFromCompanyFK.CompanyName}" FontWeight="Bold" /> 
1

如何具有分级的结合(即通过基准从父控制其他表),并设定的名称中,其将行(从列表项的数据源)的DataTemplate中一个multibinding,一起在其他表中的字段内搜索ID?

<UserControl x:Class="local:MyUserControl"> 
    <ListBox ItemsSource="{Binding Items}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Label> 
        <Label.Content> 
         <MultiBinding Converter="{StaticResource myConverter}"> 
          <Binding Path="OtherId" /> 
          <Binding 
           RelativeSource="RelativeSource FindAncestor, 
            AncestorType={x:Type local:MyUserControl}}}" 
           Path="OtherTable" /> 
         </MultiBinding> 
        </Label.Content> 
       </Label> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</UserControl> 

在转换器:

public object Convert(object[] values, Type targetType, object parameter, 
    System.Globalization.CultureInfo culture) 
{ 
    // TODO: Input type checks 

    // TODO: Castings, find key in other table, return relevant field 
    return (values[1] as IDictionary<String, String>)[values[0] as String]; 
} 
+1

听起来很不错,能否提供任何2个表格的简单示例。 – 2010-04-13 08:04:33