views:

678

answers:

1

Hi, I have a UserControl, default one generated by VS, only TextBlock is added:

<UserControl x:Class="MyNameSpace.Presentation.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="myControl">
    <Grid>
        <TextBlock x:Name="SomeTextBox" Text="SomeText"></TextBlock>
    </Grid>

Now, I put several instances of this control into parent control's WrapPanel dynamically from the code behind. I want to handle all Left Mouse Button clicks from MyControl instances. I have following code:

<UserControl x:Class="Minimo.Presentation.FirstParent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Presentation="clr-namespace:Minimo.Presentation"
Height="300" Width="300">
<WrapPanel Name="wrapPanelOfMyControls" MyControl.MouseLeftButtonDown="WrapPanel1_OnMouseLeftButtonDown">
</WrapPanel>

In the event handler I make some action, and it works. However I get the following error when editing the XAML file: The attachable property 'MouseLeftButtonDown' was not found in type 'MyControl'. How to fix this?

+1  A: 

This is just a bug with the XAML compiler/designer and can be safely ignored. However, you may be able to "fix" it by specifying a type it is more intimately aware of:

UIElement.MouseLeftButtonDown="WrapPanel1_OnMouseLeftButtonDown"

HTH, Kent

Kent Boogaart
Thanks, that solved the problem. On the other hand doesn't it make the wrap panel handler to handle all the child controls' mouse clicks events, even not from MyControl instances? (Whereas typing explicitly MyControl.MouseLeftButtonDown only from this control)
Both forms are doing exactly the same thing - they're just referring to the same routed event in different ways. And because it's a routed event, the panel will always get a chance to handle it if a child didn't already mark the PreviewMouseLeftButtonDown event as handled.
Kent Boogaart