学习WPF并来自Windows表单!
我目前正在执行以下操作来加载数据库,但是我还想知道如何将复选框绑定到列表视图项的选定事件,并在单击按钮后从sqlite数据库和列表视图中删除所选行。
编辑:感谢AVK Naidu的帮助,它现在正在运作!
public ObservableCollection<MyItem> myItems { get; set; }
public class MyItem
{
public string Key { get; set; }
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Key3 { get; set; }
public bool IsSelected { get; set; }
}
private void LoadDatabaseButton_Click(object sender, RoutedEventArgs e)
{
myItems = new ObservableCollection<MyItem>();
SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
SQLiteCommand readdatabase = new SQLiteCommand("Select * From TableName", m_dbConnection);
using (SQLiteDataReader read = readdatabase.ExecuteReader())
{
while (read.Read())
{
listView4.Items.Add(new MyItem { Key = read["Key"].ToString(), Key2 = read["Key2"].ToString(), Key3 = read["Key3"].ToString(), Key4 = read["Key4"].ToString() });
}
}
m_dbConnection.Close();
}
这是我在XAML中用于加载数据库的列表视图:
<ListView x:Name="listView4" HorizontalAlignment="Left" Height="186" VerticalAlignment="Top" Width="432" BorderBrush="Gray">
<ListView.View>
<GridView>
<GridViewColumn Header="Key" DisplayMemberBinding="{Binding Key}"/>
<GridViewColumn Header="Key1" DisplayMemberBinding="{Binding Key1}"/>
<GridViewColumn Header="Key2" DisplayMemberBinding="{Binding Key2}"/>
<GridViewColumn Header="Key3" DisplayMemberBinding="{Binding Key3}"/>
</GridView>
</ListView.View>
</ListView>
从数据库中删除:
private void RemoveRowButton_click(object sender, RoutedEventArgs e)
{
foreach (var Checkboxitem in myItems)
{
if (Checkboxitem.IsSelected == true)
{
MessageBox.Show(Checkboxitem.Key.ToString() + Checkboxitem.Key1.ToString() + Checkboxitem.Key2.ToString() + Checkboxitem.Key3.ToString());
}
}
}
答案 0 :(得分:1)
将您的MyItem
更改为以下。
public class MyItem
{
public bool IsSelected { get; set; }
public string Key { get; set; }
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Key3 { get; set; }
}
在GridView中添加CheckBox列。你的ListView将类似于下面。
<ListView x:Name="listView4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Gray">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Key" DisplayMemberBinding="{Binding Key}"/>
<GridViewColumn Header="Key1" DisplayMemberBinding="{Binding Key1}"/>
<GridViewColumn Header="Key2" DisplayMemberBinding="{Binding Key2}"/>
<GridViewColumn Header="Key3" DisplayMemberBinding="{Binding Key3}"/>
</GridView>
</ListView.View>
</ListView>
如果你从CheckBox
查看GridViewColumn.CellTemplate
,我就使用了Mode = TwoWay,以便它可以更新从中绑定数据的集合。
所以对于如下的样本数据,
myItems = new ObservableCollection<MyItem>();
for (int i = 0; i < 20; i++)
{
myItems.Add(new MyItem() { Key = i.ToString(), Key2 = i.ToString(), Key1 = i.ToString(), Key3 = i.ToString(), IsSelected = false });
}
listView4.ItemsSource = myItems;
如果您在删除按钮上重新访问myItems,则可以看到,如果选中该复选框,您的实际收藏集IsSelected
将更新为true
。现在,您可以遍历集合并删除IsSelected
为true
的