MVVM Light EventToCommand无法传递命令参数

时间:2017-12-05 00:04:21

标签: c# wpf canvas mvvm-light

请帮我解决这个问题。我差不多两天都试图解决这个问题。我一直在寻找类似于我的问题,并试图为建议的解决方案工作,但错误仍然是相同的。我想将对象canvas传递给我的视图Model,当我单击Image控件但它不起作用时。

收到我的视图模型

System.Windows.Input.MouseButtonEventArgs

这是我的观点

<Grid>
            <Image  x:Name="img"  Source="{Binding ImagePath, Source={x:Static vm:DrawingVM.instance}, Converter={StaticResource nullImageConverter}}" Stretch="None" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDown">
                        <command:EventToCommand CommandParameter="{Binding ElementName=cnvas}" Command="{Binding MouseDownCommand, Source={x:Static vm:ResizerVM.instance}}" PassEventArgsToCommand="True" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Image>

            <ItemsControl ItemsSource="{Binding ListRectangle, Source={x:Static vm:DrawingVM.instance}}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas   x:Name="cnvas" Width="{Binding ElementName=img, Path=ActualWidth}" 
                        Height="{Binding ElementName=img,Path=ActualHeight}"
                        LayoutTransform="{Binding ElementName=img, Path=LayoutTransform}" >
                        </Canvas>
                    </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="Blue"  Fill="Transparent" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>

这就是它在运行时的样子

enter image description here

这是我的View Model的代码

public RelayCommand<object> MouseDownCommand { get; private set; }
        public ResizerVM()
        {
            MouseDownCommand = new RelayCommand<object>(MouseDownEvent);
        }

        private void MouseDownEvent(object obj)
        {
            try
            {
                Console.WriteLine("Called");
                if (obj == null) return;
                Console.WriteLine(obj.GetType());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                throw ex;
            }
        }

当我将Trigger添加到我的Canvas时,它不会执行。它仅在我单击画布中绘制的矩形时执行。这与我需要的相反

0 个答案:

没有答案