c# - Windows Phone 的复选框

标签 c# sqlite windows-phone-7 checkbox windows-phone-8

我在应用程序中使用了 4 个复选框,并创建了一个整数列表,以便在选中该复选框时添加值。该值将使用 sqlite 存储在我的数据库中。这是我的代码:

List<int> hobid = new List<int>();

 private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            if (Convert.ToBoolean(cricket.IsChecked))
            {
                hobid.Add(1);
            }
            else if (Convert.ToBoolean(football.IsChecked))
            {
                hobid.Add(2);
            }
            else if (Convert.ToBoolean(music.IsChecked))
            {
                hobid.Add(3);
            }
            else if (Convert.ToBoolean(reading.IsChecked))
            {
                hobid.Add(4);
            }

int rec;
   string strInserthob = "Insert into User_hobbies (User_id,Hobbies_id) values (@User_id,@Hobbies_id)";

//Here insertion is done..

 for (int i = 0; i < hobid.Count; i++)
                {
                    User_hobbies txt = new User_hobbies
                    {
                        User_id = userid,
                        Hobbies_id = hobid[i]
                    };
                    rec = (Application.Current as App).db.Insert<User_hobbies>(txt, strInserthob);
                }

}

您能否建议我如何在选中复选框的情况下获取列表中的值。

最佳答案

我假设按钮点击是“保存”操作。

在这种情况下,由于 else if 语句,您仅保存第一个匹配项。真正快速但令人讨厌的方法是删除“if else”语句并将其替换为直接的 if。

 if (Convert.ToBoolean(cricket.IsChecked))
            {
                hobid.Add(1);
            }
 if (Convert.ToBoolean(football.IsChecked))
            {
                hobid.Add(2);
            }
 if (Convert.ToBoolean(music.IsChecked))
            {
                hobid.Add(3);
            }
 if (Convert.ToBoolean(reading.IsChecked))
            {
                hobid.Add(4);
            }

关于c# - Windows Phone 的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879026/

相关文章:

c# - 根据条件将行从一个 IEnumerable 添加到另一个

c# - WebBrowser.Document.Body 始终为空

c# - NHibernate SchemaExport 不创建表

sql - 如何在 SQLite 中的特定条件下进行 SQL 查询,将一个表的结果行与另一表的行组合起来

ruby-on-rails - Rake 正在中止,因为 sqlite db 的未定义方法 `inet'

sqlite - 将sqlite数据库与app和stringByAppendingPathComponent bundle 在一起

windows-phone-7 - Windows 8 中是否有 Windows Phone 'User Idle Detection' 的类似版本?

c# - 处理枚举更改方法的更好方法?

c# - Task<T> 卡住 UI 线程

silverlight - 当图像是 xaml 中的数据绑定(bind)时清除图像缓存(释放内存)