WPF中的CollectionViewSource不显示来自DataTable的数据



数据表中的数据没有填充组合框或文本框。来自SQL Server数据库的数据用于填充数据表,这是工作良好,我认为CollectionViewSource是好的,以及,但绑定到显示元素没有显示。

我最初从TableAdapter设计器创建了视图,一切正常,数据显示出来,我可以在表行之间移动。我需要编写一个到数据源的SQL连接的代码,因此我创建了一个SQL数据适配器,用它来填充一个数据集,然后从该数据集创建一个数据表。通过调试器,我可以看到数据表("compDataTable")包含了所有的数据。然后我使用这个DataTable作为CollectionViewSource ('tbl_CompsViewSource')绑定到我的视图控件,与原来的TableAdapter一样,但是当我运行它时,视图控件是空白的。我试图通过系统调试绑定。诊断,但它不会显示任何错误,除非它试图填充视图控件。抛出的错误是:

System.Windows。数据错误:40:BindingExpression路径错误:'Name'属性在'对象''Char' (HashCode=4325442)'上找不到。BindingExpression:路径=名称;DataItem ="字符"(HashCode = 4325442);目标元素是'ComboBox' (Name='nameComboBox');目标属性为'NoTarget'(类型为'Object')

System.Windows。数据错误:40:BindingExpression路径错误:'值'属性在'对象''EnumerableCollectionView' (HashCode=28278595)'上找不到。BindingExpression:路径=价值;DataItem = ' EnumerableCollectionView ' (HashCode = 28278595);目标元素是'TextBox' (Name='valueTextBox');目标属性为"Text"(类型为"String")

XML代码为:

<Window x:Class="CompsTabAdapt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="MainWindow" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="tbl_CompsViewSource" Source="{Binding Source=compDataTable}" diag:PresentationTraceSources.TraceLevel="High"/>
</Window.Resources>
<StackPanel DataContext="{Binding tbl_CompsViewSource}">
<Label Content="Compound :"/>
<ComboBox x:Name="nameComboBox" DisplayMemberPath="Name" ItemsSource="{Binding}" IsEditable="True">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<StackPanel Orientation="Horizontal">
<Label Width="120">Value: </Label>
<TextBox x:Name="valueTextBox" Text="{Binding Value, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
</StackPanel>
</StackPanel>
</Window>

,后面的代码是:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace CompsTabAdapt
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SqlDataAdapter CompsDataAdapt = new SqlDataAdapter();
DataSet compDataSet = new DataSet();
DataTable compDataTable = new DataTable();
CollectionViewSource tbl_CompsViewSource = new CollectionViewSource();

private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
string dbcon = ConfigurationManager.ConnectionStrings["ConnOrigString"].ConnectionString;
using (SqlConnection Connection = new SqlConnection(dbcon))
{
Connection.Open();
CompsDataAdapt = new SqlDataAdapter("SELECT * FROM tbl_Comps", Connection);
CompsDataAdapt.Fill(compDataSet);
Connection.Close();
compDataTable = compDataSet.Tables[0];
}
}
catch
{
dbConnect();
}
finally
{
tbl_CompsViewSource = (CollectionViewSource)(this.FindResource("tbl_CompsViewSource"));
tbl_CompsViewSource.View.MoveCurrentToFirst();
}
}
}

System.Windows.Data Error: 40 : BindingExpression path error: 'Value' property not found on 'object' ''EnumerableCollectionView' (HashCode=28278595)'. BindingExpression:Path=Value; DataItem='EnumerableCollectionView' (HashCode=28278595); target element is 'TextBox' (Name='valueTextBox'); target property is 'Text' (type 'String')这条消息告诉你的是,它没有在名为"valueTextBox"的TextBox绑定的源上找到属性Value。查看xaml<TextBox x:Name="valueTextBox" Text="{Binding Value, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>,没有为绑定定义源,因此源将是它的数据上下文(或者在本例中,具有设置的数据上下文的第一个父节点的数据上下文)。这是stackpanel

<Window.Resources>
<CollectionViewSource x:Key="tbl_CompsViewSource" Source="{Binding Source=compDataTable}" diag:PresentationTraceSources.TraceLevel="High"/>
</Window.Resources>
<StackPanel DataContext="{Binding tbl_CompsViewSource}">

所以源是在后面的代码中定义的compDataTable(您可能应该显式地设置访问修饰符,我很惊讶地看到它是可访问的)。

在后面的代码中,我可以看到compDataTable = compDataSet.Tables[0];因此,compDataTable的类型是Table,但类型Table没有名为Value的属性,因此绑定失败。

关于另一个错误,您将compDataTable视为一个集合,并试图访问其中一个项目的Name属性,这当然会失败。

我猜你是想访问这个表的行,所以你应该检查这个链接,它有一个如何访问数据的例子。

foreach(DataTable table in dataSet.Tables)
{
foreach(DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
//Add it to a list instead and bind the list
Console.WriteLine(row[column]);
}
}
}

*链接

中的示例

最新更新