在开发由数据集填充的listview控件时,我遇到了一个问题,该数据集是从数据库加载的。我可以成功加载它但现在我想这样做当用户将单词键入特定时将通过单词enter user设置selected = true。像捕获之类的东西:
我将提供一些代码,以便更轻松地完成工作。 只是我设计的一些概念 XAML:
<Window x:Class="WpfApplication25.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView Height="161" HorizontalAlignment="Left" Margin="38,78,0,0" Name="listView1" VerticalAlignment="Top" Width="437" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="38,49,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,49,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
守则背后:
protected void RefreshCategory()
{
SqlConnection conn;
string connStr = ConfigurationManager.ConnectionStrings["house"].ConnectionString;
conn = new SqlConnection(connStr);
conn.Open();
String strSearch = "Select * from BudgetCategories WHERE CategoriesMonth='" + BudgetMonth + "' AND CategoriesYear='" + BudgetYear + "' ORDER BY CategoriesType DESC";
SqlCommand comm = new SqlCommand(strSearch, conn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
CategoriesListView.DataContext = dt.DefaultView;
}
}
希望收到回复...谢谢你和祝你有愉快的一天:)
答案 0 :(得分:1)
这里的想法,在TextBox的TextChanged事件中搜索listView1.Items中的匹配项。如果任何项匹配,则将listView1的Selected Index设置为该匹配项的索引。
处理TextChanged事件的注册方法:
<TextBox Height="23" HorizontalAlignment="Left" Margin="38,49,0,0"
Name="textBox1" VerticalAlignment="Top" Width="120"
TextChanged="TextBox_TextChanged"/>
搜索匹配项并将所选索引设置为匹配项(如果有):
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var indexOfMatchedItem = -1;
foreach (var item in listView1.Items)
{
if (item.ToString() == textBox1.Text)
{
indexOfMatchedItem = listView1.Items.IndexOf(item);
break;
}
}
if (indexOfMatchedItem != -1)
{
listView1.SelectedItems.Clear();
listView1.SelectedIndex = indexOfMatchedItem;
}
}
我在这里使用List of string作为ListView的ItemsSource,你需要做一些调整才能使它与DataTable一起使用。
public Window()
{
InitializeComponent();
var dt = new List<string>();
dt.Add("one");
dt.Add("two");
dt.Add("three");
listView1.ItemsSource = dt;
}
另一个提示,因为您正在使用DataTable,您可以更改此行:
if (item.ToString() == textBox1.Text)
对此:
int columnIndex = 0; //set to index of the column displayed in ListView
if (((DataRowView)item).Row.Field<string>(columnIndex) == textBox1.Text)
希望这更接近你的需要。
答案 1 :(得分:1)
解决方案是这样的:
protected void SelectID(){
//Just a dummy value to store the matched item
int indexOfMatchedItem=0;
//This is the true essential part!!!
//I intiatl as -1 as we are using index which start from -1 then increase ...1,2,3
int index=-1;
//Seen I'm using Listview DataContext bound with database.
foreach (DataRow dtrCurrentRow in (((System.Data.DataView)CategoriesListView.ItemsSource)).Table.Rows)
{
//Retrieve the value frm the particula column:
String value = dtrCurrentRow[0].ToString().TrimEnd();
//MessageBox.Show("" + value);
//Increase every time in counter a row:
index++;
//Match whether it is true or what:
//Example: "C0001" equals "C0002":Will Not enter
//Example: "C0002" equals "C0002":Will Enter
if (value.Equals(Last_CID))
{
//I can enter here already:
//The "Index" value with specific row index will be initial to the dummy "indexOfMatchedItem"
indexOfMatchedItem = index;
//MessageBox.Show("" + indexOfMatchedItem);
break;
}
}
//When the dummpy position is equals to the current index postion in the list:Enter below code:
//Example: 1 == 1
if (indexOfMatchedItem == index)
{
CategoriesListView.SelectedItems.Clear();
CategoriesListView.SelectedIndex = indexOfMatchedItem;
}
}
答案 2 :(得分:0)
我找到了这个解决方案&amp;我得到了价值,但我不知道为什么不能解决它,但我知道它已经接近答案了...... :)
希望你们所有人能帮助我...谢谢你们。祝你今天愉快。我将在下面发布代码......
代码背后:
protected void SelectID(){
var indexOfMatchedItem = -1;
//Seen I'm using Listview DataContext bound with database.
foreach (DataRow dtrCurrentRow in (((System.Data.DataView)CategoriesListView.ItemsSource)).Table.Rows)
{
//Retrieve the each row the first column
String value = dtrCurrentRow[0].ToString().TrimEnd();
//Try & match:True or False
if (value.Equals(Last_CID))
{
//I can enter here already:
MessageBox.Show("HI");
//This shoud be the index of the item which match the "value" string
indexOfMatchedItem = CategoriesListView.Items.IndexOf(value);
break;
}
}
//I try to see whether the indexOfMatchedItem is pointing to the correct position in the list of the index.
MessageBox.Show("" + indexOfMatchedItem);
//But at the end it still pointing to -1
//That's why I cannot enter the below code:
if (indexOfMatchedItem != -1)
{
CategoriesListView.SelectedItems.Clear();
CategoriesListView.SelectedIndex = indexOfMatchedItem;
}
}
得到任何解决方案???希望得到回应。谢谢你