Search code examples
wpf.net-coreclickgroupbox

The Property 'Click' was not found in Type 'GroupBox'. in netcore 3.1


I have a netcore 3.1 application and would like to add a click event in order to expand / contract the content of the GroupBox.

Unfortunately, Visual Studio throws the error from the title which is stating that GroupBox does not have such an event, even though it is stated so in the Microsoft reference.

Code to reproduce:

<UserControl x:Class="SoundStudio.Views.LibraryView.Library"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             <!-- xmlns:local="clr-namespace:SoundStudio.Views.LibraryView" -->
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <StackPanel>
        <Label Content="Library"></Label>
        <Button x:Name="ImportButton" Content="Import" Click="ImportButton_Click"></Button>
        <GroupBox Header="Filter V" x:Name="FilterGroupBox" Click="FilterGroupBox_Click">
            <!-- <local:Filterview></local:Filterview> -->
        </GroupBox>
    </StackPanel>
</UserControl>

Solution

  • The documentation you have posted is for Windows Forms, the one for WPF is this. The WPF GroupBox does not have a Click event, but you can use all of the Mouse* events instead like:

    • Defined on UIElement:
      • MouseLeftButtonDown
      • PreviewMouseLeftButtonDown
      • MouseLeftButtonUp
      • PreviewMouseLeftButtonUp
      • ...
    • Defined on Control
      • MouseDoubleClick
      • PreviewMouseDoubleClick
    <GroupBox Header="Filter V" x:Name="FilterGroupBox" MouseLeftButtonDown="FilterGroupBox_Click">
       <!-- <local:Filterview></local:Filterview> -->
    </GroupBox>
    

    From another perspective, you might be using the wrong control in the first place.

    [...] add a click event in order to expand / contract the content of the GroupBox.

    In this case, you could use an Expander, which is exactly designed for this purpose.