I have this rather simple app, where it will populate the spesific Module with objects, called: Question. This object has public attributes, such as: String question {get; set;}, and int selectedAnswer{get; set;}.
What I would like to achieve, is that when the user presses eather YesButton or NoButton -button, that would change the selectedAnswer inside that specific object to 0-2. In this case, if the user pressses No, that value inside this object changes to 1, if Yes, it will turn into 2. Default is 0.
How can I refer to that object, at the Clicked-eventhandler? Here's the XML page:
<ContentPage.Content>
<ListView ItemsSource="{Binding Modules}"
x:Name="QuestionsList_module"
HasUnevenRows="True"
Margin="10,10" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Margin="10, 20, 0, 10" >
<Label Text="{Binding name}"/>
<ListView ItemsSource="{Binding questionList}"
x:Name="Item"
HasUnevenRows="True"
Margin="30,30">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="0,10,0,10">
<Label Text="{Binding question}"></Label>
<Grid HorizontalOptions="Center" Padding="0,20,0,10">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Margin="0,0,20,0" x:Name="YesButton" Text="YES" FontAttributes="Bold" BorderColor="LightGreen" BorderWidth="5" CornerRadius="20" Clicked="YesButton_Clicked" />
<Button Margin="0,0,20,0" Grid.Column="1" x:Name="NoButton" Text="NO" FontAttributes="Bold" BorderColor="HotPink" BorderWidth="5" CornerRadius="20" />
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
And here's my so far attempt to get the information inside the sender:
private void YesButton_Clicked(Object sender, EventArgs e)
{
var button = (Button)sender;
Grid listview = (Grid)button.Parent;
StackLayout ss = (StackLayout)listview.Parent;
}
And like this, I'm able to get the upper label, but still not gaining "access" to the object itself.
Please note, that all Modules are type: ObservableCollection and all Question-objects are also in ObservableCollection and the final Model will look like this:
new Module{
name = "this is the name of this module",
description = "Here goes the description",
isVisible = false,
ModuleIsChecked = false,
questionList = "And here's the list of those questions in ObservableCollection -list"}
use the BindingContext
of the button to get the bound object, then cast it to the correct type
var button = (Button)sender;
// cast to the appropriate class
var item = (Question)button.BindingContext;
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.