从 XAML 运行 MVVM 中的 ItemsControl 项目的功能

问题描述 投票:0回答:0

我正在为 MVVM 使用 Caliburn.Micro 在 C# WPF 中做一个简单的应用

我想执行一个 ItemsControl 项目的功能,但在单击一个 Rectangle 时出现下一个异常

System.Exception: 'No target found for method ChangeColor.

这是XAML代码

<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>

ItemsSource 是一个 ObservableCollection,它包含一个我创建的对象,它只有几个变量,例如 Rectangle 元素中的绑定变量和 ChangeColor 函数

我想这与当前上下文有关,但我不明白为什么,当绑定变量 Width 和 Height 工作正常时,尽管它们与 ChangeColor 函数位于同一对象中

c# wpf caliburn.micro
© www.soinside.com 2019 - 2024. All rights reserved.