Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assign x:Name property to a checkbox in source code [Xamarin/C#]

Hello I have set of check boxes that are generated from source code and not in XAML. Now, I wanted to set x:Name during creation of it so that I can loop through each element using the FindByName. I planned on setting name like checkbox1, checkbox2, checkbox3…

So that I can use this FindByName

        foreach (var  qaItem in question.Entity.QuestionAnswer)
        {
            (FindByName($"checkbox{qaItem.OrderNum}") as CheckBox).IsChecked = false;
        }

Is there a way to set "x:Name" in source? since the number of checkboxes is not definite.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

x:Name is not actually a property, it is just a XAML helper that is used to create a C# variable name. If you are creating the elements at runtime, you need to maintain an array or list in order to keep track of them

you could do something like this

Dictionary<string,Checkbox> myCheckboxList = new Dictionary<string,Checkbox>();

then as you create each checbox

var cb = new Checkbox();
... set properties, etc ...
myCheckboxList.Add(qaItem.OrderNum, cb);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading