简体   繁体   中英

How do I hide the header in a WPF Expander?

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?

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.

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
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM