I have a business requirement that when the user clicks a series of checkboxes in a WPF application that it will show panels depending upon what checkboxes they select. I would like to use the expander panel but I am not sure how to hide the header. The user should not be allowed to see it unless they check the checkbox. Does anyone know?
Asked
Active
Viewed 9,410 times
3 Answers
6
You can do this via making a custom Style for your Expander.
However, it might be easier to just place the controls in a different panel, and set it's visibility to Collapsed in response to the check box state. The main reason to use Expander is specifically to have the header and controls.

Reed Copsey
- 554,122
- 78
- 1,158
- 1,373
-
I am new to WPF. I didn't realize the collapsed option existed off the visibility. Thanks! – Greg Finzer Jan 18 '11 at 20:22
-
@Greg: No problem. Collapsed is nice since it hides and removes the space of the item - perfect for your situation. – Reed Copsey Jan 18 '11 at 20:23
-
For anyone who is stumbling across this... and is then wondering "How do I remove a control from an Expander?"... Do `expander.Content = null;` – poy Nov 12 '13 at 12:54
2
While not the ideal approach you could go this route...
<Expander>
<Expander.Header>
<TextBlock Visibility="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type Expander}, Mode=FindAncestor},
Converter={StaticResource BoolToVisibilityConverter}}">My Expander</TextBlock>
</Expander.Header>
</Expander>
...where BoolToVisibilityConverter
is something like...
public class BoolToVisibilityConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
return Visibility.Visible;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((Visibility)value == Visibility.Visible)
return true;
return false;
}
#endregion
}

Aaron McIver
- 24,527
- 5
- 59
- 88
-
1Fortuitously, WPF already has a [BooleanToVisibilityConverter](http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx) class built in. Nothing wrong with demonstrating an example, though. :) – Dan J Jan 18 '11 at 20:49
0
This answer has all that you need: WPF Expander Button Styled so it is inside Expander Header
If you use the style he mentions and set no header content, the header disappears

Community
- 1
- 1

Gustavo Muenz
- 9,278
- 7
- 40
- 42