Can someone point to an example of how to programmatically create & use Radio Buttons in C# WPF?
So basically how to (a) create them programmatically, and (b) how to catch triggers when the value changes, (c) how to pick up results at a given time.
Will be interested to see too if the answer will be based on use of a Binding approach too. If data binding is the easiest way to go then an example of this would be great. Else if data binding isn’t necessary the best/easiest way to go then a non-data-binding based example would be good.
Notes:
-
Note that the parent node I have
currently is StackPanel, so an aspect
of the question is how to add
multiple RadioButtons to a
StackPanelI guess. -
Should point out that I won’t know how many radio buttons there will be at compile time, nor what the text will be this will be discovered at run time.
-
It is a WPF application (i.e. desktop, not a web app)
Normally, we use RadioButtons to present an Enum data type to the user. And what we usually do is use an ItemsControl to present a group of RadioButtons, with each one bound to a ViewModel.
Below is a sample application I just wrote that demonstrates how RadioButtons could be used in two ways: the first is somewhat of a direct approach (and this may answer your questions above), and the second one uses an MVVM approach.
BTW, this is just something I wrote quickly (yeah, I got a lot of time in my hands) so I won’t say that everything in here is the perfect way of doing things. But I hope you find this helpful:
XAML:
Code-behind: