![](/img/trans.png)
[英]WPF ListView with group of RadioButtons and select default value
[英]How to implement yes-no radiobuttons in a listview (WPF)?
我有一个listview,其中有一个问题字段和两个单选按钮-是和否。 我想将每个问题的答案添加到数据库中。 我无法这样做。
这是我尝试过的-
XAML代码的一部分(列表视图位于stackpanel内部)-
<ListView HorizontalAlignment="Left" x:Name="listviewQue" Background="Azure"
ItemsSource="{Binding Questions}" ScrollViewer.CanContentScroll="False" SelectionMode="Multiple">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock DockPanel.Dock="Top" x:Name="quebox" Text="{Binding que_text}"
Style="{StaticResource ResourceKey=textblock_style }" />
<RadioButton GroupName="answergrp" Click="Yesradiobtn_Click" x:Name="yesradiobtn" DockPanel.Dock="Left">yes</RadioButton>
<RadioButton GroupName="answergrp" Click="Noradiobtn_Click" x:Name="noradiobtn" DockPanel.Dock="Right">no</RadioButton>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
后面的代码:
private void Yesradiobtn_Click(object sender, RoutedEventArgs e)
{
string ans = "yes";
Question q = new Question();
q = listviewQue.SelectedItem as Question;
string res = SQLiteDataAccess.AddResponse(surveryId, emp[empIndex].Id, q.que_id, ans);
if (!(res == SQLiteDataAccess.SUCCESS))
{
MessageBox.Show("SQLite Exception for 'yes' radiobutton click " + res);
}
}
private void Noradiobtn_Click(object sender, RoutedEventArgs e)
{
string ans = "no";
Question q = new Question();
q = listviewQue.SelectedItem as Question;
string res = SQLiteDataAccess.AddResponse(surveryId, emp[empIndex].Id, q.que_id, ans);
if (!(res == SQLiteDataAccess.SUCCESS))
{
MessageBox.Show("SQLite Exception for 'no' radiobutton click : " + res);
}
}
这是用于插入数据库的sqlite函数:
public static string AddResponse(string sid,string eid,int qid,string answer)
{
try
{
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
cnn.Execute("insert into RESPONSE values ('" + sid + "','" + eid + "'," + qid + ",'" + answer + "')");
}
return SUCCESS;
}
catch(Exception e)
{
return FAILURE +": exception e = "+e;
}
}
我收到的异常消息是-
\System.NullReferenceException: 'Object reference not set to an instance of an object.'
q was null.
我该如何实现?
更新问题类-
namespace Version2._0
{
public class Question
{
public int que_id { get; set; }
public string que_text { get; set; }
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
// SQLiteDataAccess.AddResponse(surveryId, emp[empIndex].Id, item.que_id, ans);
// how can I get the surveyId and emp[empIndex].Id here
}
}
}
}
这就是我解决问题的方法,这一切都要感谢@Clemens(非常感谢!)我的问题类-
public class Question
{
public int que_id { get; set; }
public string que_text { get; set; }
public Boolean IsSelected { get; set; }
}
我的XAML部分:
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock DockPanel.Dock="Top" x:Name="quebox" Text="{Binding que_text}"
Style="{StaticResource ResourceKey=textblock_style }" />
<RadioButton IsChecked="{Binding IsSelected}" x:Name="yesradiobtn" DockPanel.Dock="Left">yes</RadioButton>
<RadioButton x:Name="noradiobtn" DockPanel.Dock="Right">no</RadioButton>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
然后选择所有答案后,我点击按钮将其插入数据库中,
private void Button_Click(object sender, RoutedEventArgs e)
{
List<Question> items = (List<Question>)listviewQue.ItemsSource;
int count = 0;
int totalcnt = items.Count;
foreach (Question item in items)
{
if (item.IsSelectedRadioBtn)
{
count++;
string ans = "yes";
string res = SQLiteDataAccess.AddResponse(surveryId, emp[empIndex].Id, item.que_id, ans);
if (!(res == SQLiteDataAccess.SUCCESS))
{
MessageBox.Show("SQLite Exception for 'yes' radiobutton click " + res);
}
}
else
{
count++;
string ans = "no";
string res = SQLiteDataAccess.AddResponse(surveryId, emp[empIndex].Id, item.que_id, ans);
if (!(res == SQLiteDataAccess.SUCCESS))
{
MessageBox.Show("SQLite Exception for 'no' button click " + res);
}
}
}
if (count != totalcnt)
{
MessageBox.Show("Please enter the response for all the questions");
}
else
{
MessageBox.Show("Response recorded successfully");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.