如何检查为xml文件中的每个节点单击了列表视图中的哪个单选按钮
本文关键字:单击 节点 列表 视图 单选按钮 何检查 检查 xml 文件 | 更新日期: 2023-09-27 18:08:44
C#:
XDocument xdoc = XDocument.Load("http://quizgyan.site40.net/Questions3.xml");
var data = from query in xdoc.Descendants("question")
select new pq3(
(string)query.Element("qno"),
(string)query.Element("que"),
(string)query.Element("optiona"),
(string)query.Element("optionb"),
(string)query.Element("optionc"),
(string)query.Element("optiond"),
(string)query.Element("correct"));
listView.ItemsSource = data;
XAML:
<ListView x:Name="listView" ItemsSource="{Binding}" Margin="46,0,44,95" Background="{x:Null}" Foreground="#FFF9F6F6">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="46,36,0,0" TextWrapping="Wrap" Text="{Binding qNO}" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" Margin="46,36,0,0" TextWrapping="Wrap" Text="{Binding Que}" VerticalAlignment="Top"/>
</StackPanel>
<StackPanel x:Name="SP1" Orientation="Horizontal">
<RadioButton x:Name="RadioButton1" Content="{Binding opA}" Margin="46, 40,0, 0"/>
<RadioButton x:Name="RadioButton2" Content="{Binding opB}" Margin="46, 40,0, 0"/>
<RadioButton x:Name="RadioButton3" Content="{Binding opC}" Margin="46, 40,0, 0"/>
<RadioButton x:Name="RadioButton4" Content="{Binding opD}" Margin="46, 40,0, 0"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
您可以将属性添加到pq3类:
public bool IsChecked { get; set; }
然后绑定到UI上的该属性。。。
<RadioButton x:Name="RadioButton1" IsChecked="{Binding IsChecked}" Content="{Binding opA}" Margin="46, 40,0, 0"/>
一旦他们提交了表单(假设你已经设置了按钮点击事件(,你所要做的就是查看ItemSource,然后你就做好了准备:
foreach(var item in (IEnumerable<pq3>)listView.ItemsSource)
{
var isThisOneChecked = item.IsChecked;
}