简体   繁体   中英

How to check which radiobuttton in the listview is clicked for every node from the xml file

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>

You can add a property to your pq3 class:

public bool IsChecked { get; set; }

Then bind to that property on the UI ...

<RadioButton x:Name="RadioButton1" IsChecked="{Binding IsChecked}" Content="{Binding opA}" Margin="46, 40,0, 0"/>

And once they submit the form (assuming you have a button click event set up), all you have to do is take a look at the ItemSource and you're all set:

foreach(var item in (IEnumerable<pq3>)listView.ItemsSource)
{
    var isThisOneChecked = item.IsChecked;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM