问题描述
来自 DataTable 的数据不会填充 ComboBox 或 TextBox。来自 sql Server 数据库的数据用于填充 DataTable,它工作正常,我认为 CollectionViewSource 也可以,但与显示元素的绑定没有显示。
我最初从 TableAdapter 设计器创建了视图,一切正常,数据显示出来,我可以在表行中移动。我需要为到数据源的 sql 连接编写代码,所以我创建了一个 sql 数据适配器,用它来填充一个数据集,然后从该数据集创建一个数据表。通过调试器,我可以看到 DataTable(“compDataTable”)包含所有数据。然后我使用这个 DataTable 作为 CollectionViewSource ('tbl_CompsViewSource') 来绑定到我的 View 控件,就像原来的 TableAdapter 一样,但是当我运行它时,View 控件是空白的。我试图通过 System.Diagnostics 调试绑定,但它没有显示任何错误,除非它尝试填充 View 控件。抛出的错误是:
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“Char”(HashCode=4325442)上找不到“Name”属性。绑定表达式:路径=名称; DataItem='Char' (HashCode=4325442);目标元素是 'ComboBox' (Name='nameComboBox');目标属性是“NoTarget”(类型“Object”)
和
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“EnumerableCollectionView”(HashCode=28278595)上找不到“Value”属性。绑定表达式:路径=值; DataItem='EnumerableCollectionView' (HashCode=28278595);目标元素是 'TextBox' (Name='valueTextBox');目标属性是“文本”(类型“字符串”)
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}"/>
没有为绑定定义源,因此源将是它的 datacontex(或者在这种情况下,具有设置 datacontext 的第一个父级的 datacontex)。这是堆栈面板
<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
属性。
我的猜测是您打算访问此表的行,因此您应该检查 this link,其中包含如何访问数据的示例。
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]);
}
}
}
*链接中的示例