I want to create a Style for a WPF ListBox that includes a Button in the ControlTemplate that the user can click on and it clears the ListBox selection.
I dont want to use codebehind so that this Style can be applied to any ListBox.
I have tried using EventTriggers and Storyboards and it has proved problematic as it only works first time and stopping the Storyboard sets the previous selection back.
I know I could use a user control but I want to know if it is possible to achieve this using only a Style.
I want to create a Style for a WPF ListBox that includes a Button
Share
It is not possible to achieve this using XAML and only the classes provided by the .NET framework. However you can still produce a reusable solution by defining a new command (call it
ClearSelectionCommand) and a new attached property (call itClearSelectionOnCommand).Then you can incorporate those elements into your style.
Example:
Example usage – XAML:
Example usage – Code Behind: