如何捕获扩展器标头单击事件

时间:2017-05-03 09:24:47

标签: c# wpf xaml events click

如何在WPF中捕获扩展器标头点击事件? 感谢

<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>

我试过这个但是如果我点击扩展器的切换箭头它就不会发现事件

1 个答案:

答案 0 :(得分:2)

您要做的是定义用于预览鼠标左键的处理程序并检查原始源作为扩展名标题中的切换按钮,其名称为“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">

代码背后......

    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");
        }
     }