我编写了以下代码来创建树视图:它从 sqlCEserver 读取数据并据此生成视图。在我进入内部 while 循环以在标题中添加子项之前,它工作正常。
private void ClientTree_Initialized(object sender, EventArgs e)
{
string moduleCat = null;
string Cname = null;
SqlCeConnection _con = new SqlCeConnection(@"Data Source=C:\Users\Family\Desktop\Tranchulas projects\ControlCenter\ControlCenter\bin\Debug\ClientDB.sdf");
_con.Open();
SqlCeDataReader _reader = null;
SqlCeDataReader _reader1 = null;
SqlCeCommand readMod = new SqlCeCommand(@"SELECT DISTINCT ModuleID FROM ClientRecord", _con);
_reader = readMod.ExecuteReader();
while(_reader.Read())
{
moduleCat = _reader["ModuleID"].ToString();
treeItem = new TreeViewItem();
treeItem.Header = moduleCat;
SqlCeCommand readName = new SqlCeCommand(@"SELECT Cname FROM ClientRecord WHERE (ModuleID = '" + moduleCat + @"')", _con);
_reader1 = readName.ExecuteReader();
while (_reader1.Read())
{
Cname = _reader["Cname"].ToString();
treeItem.Items.Add(new TreeViewItem() { Header = Cname });
ClientTree.Items.Add(treeItem);
}
}
}
它给出了这个例外:
System.Windows.Markup.XamlParseException occurred
HResult=-2146233087
Message='Initialization of 'System.Windows.Controls.TreeView' threw an exception.' Line number '10' and line position '99'.
Source=PresentationFramework
LineNumber=10
LinePosition=99
第 10 行是:
<TreeView x:Name="ClientTree" Height="350.4" Width="164.2" HorizontalAlignment="Left" VerticalAlignment="Top" Initialized="ClientTree_Initialized" />