kubiej21 kubiej21 - 3 years ago 121
C# Question

Is there a way to store an id and value to a checkbox in UWP?

I'm building an app for Windows 10 using UWP. I have a stackpanel in which I have dynamically created a series of check boxes from json. Example of json formatting can be seen below:

{"id":2,"type":"Testing1"},{"id":3,"type":"Testing2"},{"id":4,"type":"Testing4"},...


I am able to set the content of each checkbox to the "type" element of my json, however, I have yet to figure out how I should be storing the "id" so that I can use it for CRUD operations later on. Is there a way to store the "id" to the checkbox itself, or should I be doing something else? The next best idea I can think of involves dynamically creating a listview with a checkbox and textblock - where "type" would be stored to the checkbox content and "id" would be stored to the textblock text. If neither idea is ideal, then what would be a more standardized approach to what I am attempting to do?

Answer Source

You can use the Tag property of the CheckBox. You can use it to store arbitrary data, yet it does not affect the appearance of the checkbox itself.

The scenario for the Tag property is to provide an general-purpose property on all FrameworkElement classes that supports data binding, animation and styles for itself but where the property's value does not have any implicit meaning to platform subsystems like layout, app model, text, input and so on. For example, you might put a value in the Tag property that has no meaning to the FrameworkElement where it is set, but which could be useful as an ElementName binding value to some other element that uses the FrameworkElement as a DataContext and processes the Tag value in its own way. Or you might use Tag as a way for an applied style to get a value from an arbitrary FrameworkElement parent into a specific applied template using {TemplateBinding} markup extension, without requiring XAML namespace mapping of a specific instance property in app XAML.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download