views:

255

answers:

2

Is there an easy way in WPF/XAML to toggle between two types of controls in the same position in a panel? I'm wondering if there are alternatives to toggling visibilities.

In my application I have several checkboxes that are used to configure a bit field (the checkboxes toggle individual bits on or off). Sometimes it is easier for the user if he is able to enter the actual field value in a textbox (such as 0x03 if the first two checkboxes are checked).

Due to layout constraints I would prefer not to have both the checkboxes and textbox visible at the same time. I want to provide a radio button that allows him to select which "view" he wants to use for editing the values.

+2  A: 

You can put both means of entry (CheckBoxes/TextBox) in the same position by putting them both in a Panel (say, StackPanel) and setting the Visibility of either the set of CheckBoxes or TextBox to Collapsed.

There are several ways to accomplish this, but my preferred approach would be to DataBind the CheckBox and TextBox to their respective RadioButton's IsChecked property and use a IValueConverter to convert between the boolean and Visibility property.

HTH.

micahtan
A: 

Use ContentControl as a placeholder. The you can set ContentControl.Content programatically.

Ray
You wouldn't want to do this -- at least setting ContentControl.Content to a FrameworkElement. If you were going the ContentControl route, you'd be better off using DataTemplates instead of dynamically swapping out UI in the Content property.
micahtan