I'm doing a simple app in C# WPF using Caliburn.Micro for MVVM
I want to execute a function of an ItemsControl item, but I get the next exception when clicking a Rectangle
System.Exception: 'No target found for method
ChangeColor.'
Here is the XAML code
<ItemsControl ItemsSource="{Binding Lines}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="LightYellow" Margin="10 0" Width="{Binding CanvasWidth}" Height="{Binding CanvasHeight}" Focusable="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="{Binding Width}"
Height="{Binding Height}"
Stroke="Black" StrokeThickness="1"
Fill="Black"
cal:Message.Attach="[Event MouseDown] = [Action ChangeColor()]"
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The ItemsSource is an ObservableCollection that contains an object that I created that only has a couple variables like the bound in the Rectangle element, and the ChangeColor function
I suppose that it has something to do with the current context, but I can't understand why, when the bound variables Width and Height are working just fine although they are in the same object as the ChangeColor function
The only thing i see is a bad definition of your Method.
i hace tested and no problem, this is the code i have tested: in
MainViewModel.csThe method defined in xaml:
and the class Lines: