1

第一次在这里发帖很抱歉,如果我搞砸了/忘记了什么。我正在使用 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周)隐藏/摆脱标题,那也可以工作。但如果可能的话,我更喜欢以前尝试过的另一种方式。谢谢!

4

1 回答 1

0

Expander.Collapsed 事件有路由策略冒泡。在到达 Expander1 之前将事件标记为已处理,一切都应该没问题。

Private Sub Expander2_Collapsed(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander2.Collapsed
    ...
    e.Handled = True
End Sub
于 2013-06-21T15:24:10.677 回答