c# - 如何在列表视图(WPF)中实现是-否单选按钮?
问题描述
我有一个列表视图,其中有一个问题字段和两个单选按钮-是和否。我想将每个问题的响应添加到数据库中。我无法这样做。
这是我尝试过的 -
XAML 代码的一部分(列表视图位于堆栈面板内) -
<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");
}
}
推荐阅读
- r - 如何使闪亮的 selectInput 下拉菜单在多个数据帧之间进行选择
- android-edittext - `android:editable` 已弃用:使用 `
` 使其可编辑 - java - 是否可以使用 bazel 查询来输出隐式(或任何)规则的完整语法?
- python - 在由 2 个字段分组的 DataFrame 中,从第一个分组字段内的第二个分组字段计算行 n 和 n-1 之间的差异
- reactjs - 如何使用 React Router 将信息从子组件传输到父组件 React.js
- r - 用于 Leaflet/R 的 addLegend 中的组函数未正确分离图例
- c - 将函数内部的指针分配给参数并在函数内部设置指向 Null 的指针是可以的
- java - 从 JMS 队列中批量获取
- python - PYthon - 字符串列表到嵌套的字符串列表中,其中每个列表包含每个字符串的元素
- mongodb - 带有子字段的 mongo $cond