Reputation: 91
I have N radio buttons and based on radio button selection I want to display one user control out of N user controls in wpf application. I want to use less code behind and more xaml can anyone please suggest good solution for this.
Thanks,
Upvotes: 0
Views: 1141
Reputation: 19294
what you have is a list of ( caption describing a behaviour ; User Control ).
So define MyUserOptions : a class having ControlCaption and UserControl as public property.
Build a list of such 'MyUserOptions'
Display that list in a ListView, with the look you want for the selected item, the dataTemplate of MyUserOptions just shows the caption. Below that listview, put a ContentControl that has the listView SelectedItem as DataContext, and that bind to the UserControl property.
Upvotes: 0
Reputation: 5892
First, put all the controls you want to show in your XAML. (on top of each other if you want...) Then, put the following code inside of each of those controls to turn on and off visibility for your controls.
Visibility="{Binding Path=IsMyControlVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
In your code behind, implement a property for each of the controls you want to show/hide (for the above control call the property 'IsMyControlVisible'.
Then, Bind your radio button IsChecked to your IsMyControlVisible property.
IsChecked="{Binding IsMyControlVisible, Mode=OneWayToSource, FallbackValue=false }"
Lastly, if you're just using code-behind for the IsMyControlVisible property, you should put the following in your xaml for your control to set the data context to your code-behind:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Upvotes: 1