Search code examples
c#wpfclickstackpaneldisabled-control

Handle click event or MouseDown in a disabled WPF StackPanel


It's been a while I'm trying to do something like that and I am about throwing a NotImplementedException now... I mean, throwing in the towel. You are my last hope.

So, there is my goal: Enabling a StackPanel when the user has clicked on it for user-friendly purpose.

I've got some items in this disabled StackPanel which is inside a ScrollViewer inside a Grid inside a Window.

There is a part of my code for understanding:

<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="30" MaxHeight="30"/>
        <RowDefinition MinHeight="22" MaxHeight="22"/>
        <RowDefinition MinHeight="155" Height="155"/>
        <RowDefinition MinHeight="130"/>
        <RowDefinition MinHeight="25" MaxHeight="25"/>
        <RowDefinition MinHeight="22" MaxHeight="22"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="355" Width="360" />
        <ColumnDefinition MinWidth="330" />
        <ColumnDefinition MinWidth="280" />
    </Grid.ColumnDefinitions>
    <ScrollViewer Grid.Column="2" Grid.Row="3" VerticalScrollBarVisibility="Auto">
        <StackPanel IsEnabled="False" IsEnabledChanged="ioStackPanel_IsEnabledChanged">
            <Grid Height="22">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="110"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="20"/>
                </Grid.ColumnDefinitions>
            </Grid>
        </StackPanel>
    </ScrollViewer>
</Grid>

I tried something like that already:

<ScrollViewer Grid.Column="2" Grid.Row="3" VerticalScrollBarVisibility="Auto" MouseDown="ScrollViewer_MouseDown">

private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("Well done bro!");
}

But nothing is raised. No MessageBox is displayed.


Solution

  • The following code is working fine, follow it.

    <Grid x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="30" MaxHeight="30" />
            <RowDefinition MinHeight="22" MaxHeight="22" />
            <RowDefinition Height="155" MinHeight="155" />
            <RowDefinition MinHeight="130" />
            <RowDefinition MinHeight="25" MaxHeight="25" />
            <RowDefinition MinHeight="22" MaxHeight="22" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="360" MinWidth="355" />
            <ColumnDefinition MinWidth="330" />
            <ColumnDefinition MinWidth="280" />
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Row="3"
                      Grid.Column="2"
                      PreviewMouseLeftButtonDown="InnerPanel_PreviewMouseLeftButtonDown_1"
                      VerticalScrollBarVisibility="Auto">
            <StackPanel Name="InnerPanel"
                        Background="Gray"
                        IsEnabled="False"
                        IsEnabledChanged="StackPanel_IsEnabledChanged_1">
                <Grid>
    
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="110" />
                        <ColumnDefinition />
                        <ColumnDefinition Width="50" />
                        <ColumnDefinition Width="50" />
                        <ColumnDefinition Width="20" />
                    </Grid.ColumnDefinitions>
                    <Button Grid.Column="2" Content="Inner Panel" />
                </Grid>
            </StackPanel>
        </ScrollViewer>
    </Grid>
    
    
    private void InnerPanel_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
    
        }
    

    This is working fine and implement your logic in the above event.