【问题标题】:C# WPF XAML binding to DataTableC# WPF XAML 绑定到 DataTable
【发布时间】:2011-02-05 15:18:29
【问题描述】:

我有以下表格:

公司 {CompanyID, CompanyName}
交易{CompanyID, Value}

我有一个列表框:

<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 错误:39: BindingExpression 路径错误: 'companyRowByBuyFromCompanyFK' 在“对象”上找不到属性 ''DataRowView' (HashCode=30295189)'。 BindingExpression:Path=companyRowByBuyFromCompanyFK.CompanyName; DataItem='DataRowView' (哈希码=30295189);目标元素是 '文本块'(名称='');目标属性 是“文本”(输入“字符串”)

看起来它没有将 DataRowView 项转换为 SuppliersRow

最好的方法是什么?

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

  2. 为每个表创建表视图?这将是冗长的,因为我有大量具有外键的表。

【问题讨论】:

  • 如果您在绑定中添加跟踪:{Binding Path=companyRowByBuyFromCompanyFK.CompanyName, diagnostics:PresentationTraceSources.TraceLevel=High} 并在列表框绑定时观察您的输出窗口,它会说明什么?这可能会让我们更深入地了解绑定失败的原因。
  • 天哪,谢谢!我总是忘记输出窗口!!!!为什么它不显示在错误窗口中!我现在修好了:)谢谢你的提示!!!

标签: c# wpf data-binding xaml datatable


【解决方案1】:

有人杀了我....感谢您指出我总是忘记的输出窗口! :(

我通过查看以下输出窗口找到了解决方案

System.Windows.Data 错误:39: BindingExpression 路径错误: 'companyRowByBuyFromCompanyFK' 在“对象”上找不到属性 ''数据行视图

我发现 DataTemplate 绑定绑定到 DataTemplate 的 SelectedItem,它是 DataRowView 而不是实际的强类型数据。幸运的是,DataRowView 类有一个指向 DataRow(即供应商行)类的 Row 属性,因此我可以像往常一样从那里绑定。修复在后续行中。

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

将其更改为以下内容按我的预期工作。

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

【讨论】:

    【解决方案2】:

    如何在转换行的数据模板中设置多重绑定(来自列表项的数据源),以及分层绑定(从父控件传递对另一个表的引用)和字段名称要在其中搜索 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];
    }
    

    【讨论】:

    • 听起来不错,能否提供任意2个表的简单示例。
    猜你喜欢
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2011-02-28
    • 2013-07-28
    • 2011-07-15
    • 2018-02-07
    • 2013-06-20
    相关资源
    最近更新 更多