简体   繁体   中英

MouseLeftButtonDown event isn't fired in child ContentControl

I have PortItem which derived from ContentControl , TextedStackPanel derived from StackPanel which contains PortItems . And in MainWindow i have 2 StackPanels which contains TextedStackPanels . In PortItem i have overridden MouseLeftButtonDown method. But when i do this on this method isn't fired. I searched here in the forum, and found that Background property of Grid/StackPanel must be set to Transparent. I applied this, but there is no changes. What to do ?

EDIT 1 I use partial classes. I have 2 classes: PortItem.cs and PortItem.cs.xaml . I modifiy any visual changes in this XAML file.

EDIT 2 Also any mouse events aren't fired. Triggers which i use IsMouseOver are also dont work when i keep mouse on PortItem

XAML

<ContentControl x:Class="**.PortItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:UI="clr-namespace:**.UIData" Width="17" Height="17" Margin="3" SnapsToDevicePixels="True" >
<Grid Background="Transparent" Name="mainGrid">
    <!-- transparent extra space makes connector easier to hit -->
    <Rectangle Fill="Transparent" Margin="-2"/>
    <Border BorderBrush="Green" x:Name="border" BorderThickness="2">
        <Border.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=IsMouseOver}" Value="True">
                        <Setter  Property="Border.BorderBrush" Value="Blue"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter  Property="Border.BorderBrush" Value="Blue"/>
                    </DataTrigger>
                    <!--<DataTrigger Binding="{Binding ContactPort}" Value="{x:Null}">
            <Setter TargetName="border" Property="Border.BorderBrush" Value="Green"/>
        </DataTrigger>-->
                </Style.Triggers>
            </Style>
        </Border.Style>
            <Image  Source="/**;component/Resources/1337238611_port.png">
        </Image>
    </Border>
</Grid>

Edit: in light of the changes you've made to your code, try to add ClipToBounds="False" to the top of your user control declaration.

<ContentControl x:Class="**.PortItem" ClipToBounds="False"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:UI="clr-namespace:**.UIData" Width="17" Height="17" Margin="3" SnapsToDevicePixels="True" >

Have you created a template for your PortItem? I created the follow class to replicate your PortItem and break point on the base.OnMouseButtonDown line and it fires, I think the reason your method is not executing is because there is no visual element for the mouse to actually interact with, try adding the style below to your app and you should see the method fire properly.

   public class PortItem: ContentControl
    {
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {

            base.OnMouseLeftButtonDown(e);
        }
    }

then in XAML I created a style to give it something to render.

 <local:PortItem Margin="44,36,156,95">
        <local:PortItem.Style>
            <Style TargetType="{x:Type local:PortItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type local:PortItem}">
                            <Border Background="Transparent">
                                <ContentPresenter Content="{TemplateBinding Content}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </local:PortItem.Style>
    </local:PortItem>

The background being Transparent that you mention you can see in the border control, if you leave the background out you are correct, the event never fires.

Make sure that you haven't set 'IsHitTestVisible' to false on your PortItem. Also, make sure no other controls are on top of it. If they are, set their 'IsHitTestVisible' property to false and then your PortItem control will get the mouse right click event. To make sure nothing is on top, declare your put your PortItem as the last thing added to your TextedStackPanel. To double check that nothing else is on top, change the background color of other controls to something really noticeable (just for testing) to see if anything is covering your PortItem control. Also, change the color on your PortItem control to verify that it is really where you think it is. Then once you get it all working, change the colors back to their original colors.

If you could give us a code sample of your XAML, that might help. If you're adding the PortItems dynamically in code behind, supply that code too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM