C# 如何滚动到列表框中的选定项?
我正在使用存储过程添加客户。 插入后,我设法选择了新添加的客户,但我无法专注于列表框中的记录 我什么都试过了,比如:C# 如何滚动到列表框中的选定项?,c#,wpf,C#,Wpf,我正在使用存储过程添加客户。 插入后,我设法选择了新添加的客户,但我无法专注于列表框中的记录 我什么都试过了,比如: lbxCustomers.ScrollIntoView(this.lbxCustomers.SelectedIndex); 使用物品和材料进行了许多修改,但没有任何效果。 它仍然没有将视图滚动到所选项目。。 有什么想法吗 是WPF 我的init如下所示: private void init() { SqlConnection conne
lbxCustomers.ScrollIntoView(this.lbxCustomers.SelectedIndex);
使用物品和材料进行了许多修改,但没有任何效果。
它仍然没有将视图滚动到所选项目。。
有什么想法吗
是WPF
我的init如下所示:
private void init()
{
SqlConnection connection = null;
SqlDataReader reader = null;
try
{
connection = new SqlConnection(this.strConnection);
connection.Open();
SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers", connection);
SqlDataAdapter sda = new SqlDataAdapter(command);
reader = command.ExecuteReader();
ListItem listItem;
while (reader.Read())
{
listItem = new ListItem(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString());
this.lbxCustomers.Items.Add(listItem);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
}
finally
{
connection.Close();
reader.Close();
}
}
class ListItem
{
private string customerID, companyName, contactName;
public ListItem(string customerID, string companyName, string contactName)
{
this.customerID = customerID.Replace("'", "''");
this.companyName = companyName.Replace("'", "''");
this.contactName = contactName.Replace("'", "''");
}
public override string ToString()
{
return this.companyName + " (" + this.contactName + ")";
}
public string CustomerID
{
get { return this.customerID; }
}
}
init2与以下行不同:
SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers WHERE CompanyName = '" + correctID + "'", connection);
它引用的ListItem如下所示:
private void init()
{
SqlConnection connection = null;
SqlDataReader reader = null;
try
{
connection = new SqlConnection(this.strConnection);
connection.Open();
SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers", connection);
SqlDataAdapter sda = new SqlDataAdapter(command);
reader = command.ExecuteReader();
ListItem listItem;
while (reader.Read())
{
listItem = new ListItem(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString());
this.lbxCustomers.Items.Add(listItem);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
}
finally
{
connection.Close();
reader.Close();
}
}
class ListItem
{
private string customerID, companyName, contactName;
public ListItem(string customerID, string companyName, string contactName)
{
this.customerID = customerID.Replace("'", "''");
this.companyName = companyName.Replace("'", "''");
this.contactName = contactName.Replace("'", "''");
}
public override string ToString()
{
return this.companyName + " (" + this.contactName + ")";
}
public string CustomerID
{
get { return this.customerID; }
}
}
将代码更改为:
ListItem listItem = null;
while (reader.Read())
{
listItem = new ListItem(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString());
this.lbxCustomers.Items.Add(listItem);,
lbxCustomers.SelectedItem = listItem;
}
向我们展示将其添加到Listbox的代码,让我们通读一下我的这篇老博文。它谈到了聚焦ListBoxItem,但这涉及到将其滚动到视图中。诀窍是在生成项目容器并使用Dispatcher之后执行此操作。我尝试了,但最后一行抛出了错误:方法“Select”没有重载接受“0”arguments@user1938719嗯,我在windows窗体中试用过。如果没有Select(),这是否有效?否。我得到:使用未分配的局部变量'listItem'@user1938719 set listItem listItem=null;好的,但它总是将其设置为最后一项。我需要在执行Insert Customer后将其设置(并在lisy中聚焦)到我新添加的项目。我的问题是/