wpf 从XAML运行MVVM中ItemsControl项的函数

ars1skjm  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(148)

我正在用C# WPF做一个简单的应用程序,使用的是MVVM的Caliburn.Micro
我想执行ItemsControl项的函数,但单击Rectangle时出现下一个异常
System.Exception:'找不到方法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函数
我认为这与当前上下文有关,但我不明白为什么,当绑定变量WidthHeight工作正常时,尽管它们与ChangeColor函数在同一个对象中

e4eetjau

e4eetjau1#

我唯一看到的是你的方法的一个糟糕的定义。
我已经测试过了,没有问题,这是我测试过的代码:单位:MainViewModel.cs

using Caliburn.Micro;
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Shapes;

namespace UserControlSample.ViewModels
{
    public class MainViewModel : Screen
    {
        public BindableCollection<Lines> Lines { get; set; }
        private int canvasWidth;
        public int CanvasWidth
        {
            get { return canvasWidth; }
            set
            {
                canvasWidth = value;
                NotifyOfPropertyChange(() => CanvasWidth);
            }
        }
        private int canvasHeight;
        public int CanvasHeight
        {
            get { return canvasHeight; }
            set
            {
                canvasHeight = value;
                NotifyOfPropertyChange(() => CanvasHeight);
            }
        }

        public MainViewModel()
        {
            var a = new List<Lines>()
            {
                new Lines {X = 20, Y = 20, Width = 400, Height = 50}
            };
            canvasWidth = 500;
            canvasHeight = 300;
            Lines = new BindableCollection<Lines>(a);
            FileNames = new BindableCollection<string>() { "Test1", "test2" };
        }

        public void ChangeColor(Rectangle sender)
        {
            SolidColorBrush color = new SolidColorBrush(Colors.AliceBlue);
            sender.Fill = color;
        }
    }
}

xaml中定义的方法:

cal:Message.Attach="[Event MouseDown] = [Action ChangeColor($source)]"

类Lines:

namespace UserControlSample.ViewModels
{
    public class Lines
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
    }
}

相关问题