'xamarin.forms Access name or ID of checkbox in event handler
I have multiple checkboxes in my view and they are all bound to the same eventhandler. Now I need to know which checkbox was checked or unchecked inside this eventhandler.
So i tried accessing the name, and funny enough, this is what this looks like:
As you can see under ID it states the name of checkbox.
When when I try accessing that ID
var checkbox = sender as CheckBox;
checkbox.Id now has changed to a guid, and is no longer the name of the checkbox (which is something I dont understand either)
How can I pass a param or name to my checkbox to retrieve that in the event handler?
EDIT:
This is NOT workign code (as I canoot access checkbox.Id like this, as it is a guid object not just a string)
<CheckBox Color="White"
CheckedChanged="chk_monthly_CheckedChanged"
x:Name="check_daily"
HorizontalOptions="Start"
VerticalOptions="Start"/>
async void chk_monthly_CheckedChanged(System.Object sender, Xamarin.Forms.CheckedChangedEventArgs e)
{
var checkbox = sender as CheckBox;
check_daily.CheckedChanged -= chk_monthly_CheckedChanged;
chk_monthly.CheckedChanged -= chk_monthly_CheckedChanged;
chk_weekly.CheckedChanged -= chk_monthly_CheckedChanged;
await Task.Delay(50);
check_daily.IsChecked = false;
chk_monthly.IsChecked = false;
chk_weekly.IsChecked = false;
checkbox.IsChecked = true;
// not working
if(checkbox.Id == "check_daily")
{
}
check_daily.CheckedChanged += chk_monthly_CheckedChanged;
chk_monthly.CheckedChanged += chk_monthly_CheckedChanged;
chk_wee
kly.CheckedChanged += chk_monthly_CheckedChanged; }
Solution 1:[1]
OPTION 1
First name the checkbox:
<CheckBox x:Name="cbx_laser">
Then in the Event Handler Check if the sender is the checkbox
if(sender.Equals(cbx_laser)){
// Do Stuff
string cbx_name = cbx_laser.Name; // Heres how to get the name
}
OPTION 2
Use the sender param and typecast.
string cbx_name = ((Checkbox)sender).Name;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |