在WPF / VB.NET中只关闭另一个扩展器中的一个扩展器

时间:2013-06-21 14:04:01

标签: wpf vb.net visual-studio-2010 expander

首先发布在这里,如果我弄乱了什么/遗忘了什么就很抱歉。 我正在使用WPF应用程序在Visual Studio 2010中工作,并且正在使用VB.NET。

所以我有一个父扩展器,它包含一个带有两个控件的网格:另一个扩展器和另一个网格。我已经写了一些代码,使每个扩展器在折叠或关闭时(在其他一些东西中)变得不可见,并且在扩展/打开时改变颜色/变得可见。我还在其他区域放置了几个按钮来完成相同的任务。我的问题是,当我折叠第二个扩展器时,第一个(父)扩展器也会关闭/变得不可见。但是,用于折叠第二个扩展器的按钮可以完美地工作。这是我的相关代码(希望我正确格式化):

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

不要担心代码中的所有按钮,按钮一切正常。实际上,假设一个按钮与折叠扩展器完全相同,它可以正常工作。当你点击实际的扩展器来折叠它时,我只需要发生同样的事情。这是按钮的代码,所以你看它是相同的:

    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

非常感谢您的帮助,我真的很感激!

编辑:或者,如果有一种简单的方法(我对WPF很新......〜1周)来隐藏/删除标题,那也可以。但如果可能的话,我会更喜欢我以前试过的其他方式。谢谢!

1 个答案:

答案 0 :(得分:0)

Expander.Collapsed Event有路由策略Bubbling。将事件标记为在达到Expander1之前处理,所有事情都应该没问题。

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