سؤال

First post here so sorry if I mess something up/forget something. I'm working in Visual Studio 2010 with a WPF application and am using VB.NET.

So I have a parent expander that contains a grid with two controls: another expander and another grid. I have written some code behind to cause each expander to become invisible when it is collapsed or closed (among a few other things), and to change color/ become visible when it is expanded/opened. I also have a few buttons placed in other areas that accomplish the same tasks. My problem is that when I collapse the 2nd expander, the 1st (parent) expander also closes/ becomes invisible. However, a button that is used to collapse the 2nd expander works perfectly. Here is my relevant code (hopefully I'm formatting this right):

XAML

    <Expander Name="Expander1" Visibility="Hidden" >
        <Grid >
            <Grid.RowDefinitions >
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Expander Name="Expander2" Visibility="Hidden" >
                 <Content ...>
            </Expander>
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="20" />
                </Grid.RowDefinitions>
                <Content... />
            </Grid>
        </Grid>
    </Expander>

VB.NET

    Private Sub Expander2_Expanded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander2.Expanded

    Expander2.Background = Brushes.PaleTurquoise
    Expander2.BorderBrush = Brushes.Black

End Sub

    Private Sub Expander2_Collapsed(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander2.Collapsed

    Expander2.IsExpanded = False
    Expander2.Background = Brushes.Transparent
    Expander2.BorderBrush = Brushes.Transparent
    Expander2.Visibility = Windows.Visibility.Visible
    ButtonA7.Visibility = Windows.Visibility.Visible
    Expander1.IsExpanded = True
    Expander1.Background = Brushes.PaleTurquoise
    Expander1.BorderBrush = Brushes.Black
    Expander1.Visibility = Windows.Visibility.Visible

End Sub

    Private Sub Expander1_Expanded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander1.Expanded

    Expander1.Background = Brushes.PaleTurquoise
    Expander1.BorderBrush = Brushes.Black

End Sub

    Private Sub Expander1_Collapsed(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander1.Collapsed

    Expander1.Background = Brushes.Transparent
    Expander1.BorderBrush = Brushes.Transparent
    Expander1.Visibility = Windows.Visibility.Hidden
    ButtonA7.Visibility = Windows.Visibility.Visible

End Sub

Dont worry about all the buttons in the code, the buttons all work fine. In fact, one button is suppose to do the EXACT same thing as collapsing the expander, and it works properly. I just need the same thing to happen when you click on the actual expander to collapse it. Here is the code for the button so you see it is the same:

    Private Sub Button_Click_2(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    Expander2.IsExpanded = False
    Expander2.Background = Brushes.Transparent
    Expander2.BorderBrush = Brushes.Transparent
    Expander2.Visibility = Windows.Visibility.Visible
    Expander1.IsExpanded = True
    Expander1.Background = Brushes.PaleTurquoise
    Expander1.BorderBrush = Brushes.Black
    Expander1.Visibility = Windows.Visibility.Visible
    ButtonA7.Visibility = Windows.Visibility.Visible

End Sub

Thank you so much for any help, I really appreciate it!

EDIT: Alternatively, if there is an easy way (I am VERY new to WPF... ~1week) to hide/ get rid of the header, that could work too. But I would prefer it the other way where I tried before, if possible. Thanks!

هل كانت مفيدة؟

المحلول

Expander.Collapsed Event has routing strategy Bubbling. Mark event as handled before it reaches Expander1 and all should be okay.

Private Sub Expander2_Collapsed(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander2.Collapsed
    ...
    e.Handled = True
End Sub
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top