如何捕获扩展器头点击事件 [英] How to catch expander header click event
本文介绍了如何捕获扩展器头点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在WPF中捕获扩展器头点击事件?
感谢
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>
我尝试了这个,但是如果我点击expander的切换箭头 p>
I tried with this but it doesn't catch the event if I click on expander's toggle arrow
推荐答案
你想做的是定义预览鼠标左键的处理程序,并检查原始来源为切换按钮名称为HeaderSite的扩展器标题
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">
代码背后...
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");
}
}
这篇关于如何捕获扩展器头点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文