I have a WPF application with an Expander Control. I want the Expander Control to expand ONLY based on IsMouseOver event. I was able to enable the IsMouseOver behavior by implementing the corresponding trigger in ControlTemplate.Triggers. But I can't figure out how to disable the IsPressed/OnMouseClick events. Any advice on how to do is greatly appreciated. Thanks.
Asked
Active
Viewed 92 times
0
-
Could you please share your custom `ControlTemplate`? – mm8 Jun 11 '20 at 15:15
1 Answers
1
You could handle the PreviewMouseLeftButtonDown
event for the ToggleButton
that is called "HeaderSite" in the default template:
private void HeaderSite_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
=> e.Handled = true;
XAML:
<ToggleButton x:Name="HeaderSite" PreviewMouseLeftButtonDown="HeaderSite_PreviewMouseLeftButtonDown" ContentTemplate="{TemplateBinding HeaderTemplate}" ... />
Remember that XAML is a markup language and that you should implement this kind of behaviour in a programming language such as C#.
If your template is defined in a ResourceDictionary
, you should add a code-behind class to it and defined the event handler there.

mm8
- 163,881
- 10
- 57
- 88
-
Thank you mm8. Your answer makes complete sense. When i enter the code, i get the error: CS1061 'App' does not contain a definition for 'HeaderSite_PreviewMouseLeftButtonDown' and no accessible extension method 'HeaderSite_PreviewMouseLeftButtonDown' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?). I think this has to do with your second comment. My XAML Expander style definition is in App.xaml. What code do i need to add where to address the error? – scorpiotomse Jun 11 '20 at 16:02
-
@scorpiotomse: Where did you put the event handler? It the style is defined in `App.xaml`, the code goes in `App.xaml.cs`. – mm8 Jun 11 '20 at 16:07