【发布时间】:2021-06-04 11:06:34
【问题描述】:
DataTable 中的数据未填充 ComboBox 或 TextBox。来自 SQL Server 数据库的数据用于填充 DataTable,它工作正常,我认为 CollectionViewSource 也可以,但没有显示与显示元素的绑定。
我最初通过 TableAdapter 设计器创建了视图,一切正常,数据显示出来了,我可以在表格行中移动。我需要为与数据源的 SQL 连接编写代码,因此我创建了一个 SQL 数据适配器,使用它来填充数据集,然后从该数据集创建一个数据表。通过调试器,我可以看到 DataTable(“compDataTable”)包含所有数据。然后我使用这个 DataTable 作为 CollectionViewSource ('tbl_CompsViewSource') 来绑定到我的 View 控件,就像原来的 TableAdapter 一样,但是当我运行它时,View 控件是空白的。我尝试通过 System.Diagnostics 调试绑定,但它没有显示任何错误,除非它尝试填充视图控件。抛出的错误是:
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“Char”(HashCode=4325442)上找不到“名称”属性。绑定表达式:路径=名称; DataItem='Char' (HashCode=4325442);目标元素是 'ComboBox' (Name='nameComboBox');目标属性是“NoTarget”(类型“对象”)
和
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“EnumerableCollectionView”(HashCode=28278595)上找不到“值”属性。绑定表达式:路径=值; DataItem='EnumerableCollectionView' (HashCode=28278595);目标元素是'TextBox'(名称='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();
}
}
}
【问题讨论】:
标签: wpf datatable binding collectionviewsource