How to catch expander header click event

2019-07-23 06:10发布

How to catch expander header click event in WPF? Thanks

<Expander  IsExpanded="{Binding Items[0], Mode=OneWay, Converter={StaticResource expanderExpandedConverter}}" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Loaded="Expander_Loaded" >
                                        <Expander.Header>
                                            <DockPanel  PreviewMouseLeftButtonDown="Expander_MouseLeftButtonUp">
                                                <TextBlock Text="{Binding Path=Name}" Style="{StaticResource GroupStyle}" ScrollViewer.HorizontalScrollBarVisibility="Hidden"></TextBlock>
                                            </DockPanel>

                                        </Expander.Header>
                                        <Expander.Style>
                                            <Style TargetType="{x:Type Expander}">
                                                <Setter Property="TextElement.FontFamily" Value="Arial Nova"/>
                                                <Setter Property="BorderThickness" Value="0,0,0,1"/>
                                                <Setter Property="BorderBrush" Value="Black"/>
                                            </Style>
                                        </Expander.Style>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>

I tried with this but it doesn't catch the event if I click on expander's toggle arrow

1条回答
不美不萌又怎样
2楼-- · 2019-07-23 06:43

What you want to do is to define a handler for preview left mouse button up and check for the original source as the toggle button in the expander header whose name is "HeaderSite"

XAML...

    <Expander IsExpanded="{Binding Items[0], Mode=OneWay, Converter={StaticResource expanderExpandedConverter}}" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Loaded="Expander_Loaded"  PreviewMouseLeftButtonDown="Expander_PreviewMouseLeftButtonDown" 
              PreviewMouseLeftButtonUp="Expander_PreviewMouseLeftButtonUp">

code behind...

    private void Expander_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement fe = e.OriginalSource as FrameworkElement;
        if(fe is ToggleButton && fe.Name == "HeaderSite")
        {
            Trace.WriteLine("Clicked in expander header");
        }
     }
查看更多
登录 后发表回答