C# 获取WPF中单击的矩形的名称

C# 获取WPF中单击的矩形的名称,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我正在尝试获取WPF应用程序中单击的矩形的名称。 我首先尝试了这个: XAML: 它工作得很好,但是我不知道如何将selectedRect字符串传递给我的ViewModel 因此,我尝试直接将中继命令从xaml传递到Viewmodel: <UserControl x:Class="G.View.MapView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="ht

我正在尝试获取WPF应用程序中单击的矩形的名称。 我首先尝试了这个: XAML:

它工作得很好,但是我不知道如何将selectedRect字符串传递给我的ViewModel 因此,我尝试直接将中继命令从xaml传递到Viewmodel:

<UserControl  x:Class="G.View.MapView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
      xmlns:extended="clr-namespace:G.ViewModel">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".7*" />
            <ColumnDefinition Width=".3*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height=".8*" />
            <RowDefinition Height=".2*" />
        </Grid.RowDefinitions>
        <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Grid.Column="0" Grid.Row="0" Margin="10,10,0,0">
            <ItemsControl ItemsSource="{Binding CaskList}" HorizontalAlignment="Left" VerticalAlignment="Top">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Width="1680" Height="800">
                            <Canvas.InputBindings>
                                <!-- One type of implementation -->
                                <KeyBinding Key="{Binding MouseLeftButtonDown.GestureKey}" Command="{Binding Path=HandleMyEvent}"/>
                            </Canvas.InputBindings>
                        </Canvas>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ContentPresenter">
                        <Setter Property="Canvas.Left" Value="{Binding Left}"/>
                        <Setter Property="Canvas.Top" Value="{Binding Top}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Rectangle Stroke="Black" Width="64" Height="64" Fill="{Binding Color}"></Rectangle>
                            <Label Content="{Binding ID}" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>      
    </Grid>
    </UserControl>

它可以工作,当我单击画布时,我在我的方法中输入正确,但是我无法获得矩形名称,没有事件MouseButtonEventArgs e。 你能帮我吗

编辑:添加完整的xaml

尝试以下操作:

private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    string selectedRect;
    if (e.OriginalSource is Rectangle)
    {
        Rectangle ClickedRectangle = (Rectangle)e.OriginalSource;

        var mouseWasDownOn = e.Source as FrameworkElement;
        string elementName = mouseWasDownOn.Name;
        selectedRect = elementName.ToString();

        //ViewModel code
        var vm = this.ViewModel as YourViewModel;
        vm.RectangleName = elementName.ToString();
    }
}

谢谢你的帮助。我得到一个错误:我的xaml.cs不包含“ViewModel”的定义。请尝试从xaml中具有视图模型(网格或用户控件)的第一个元素获取视图模型。如果您不知道如何找到它,请在这里发布您的xamlI在我的原始问题中添加了完整的xaml。在我的代码中,我添加了var vm=this.ViewModel作为您的ViewModel;vm.RectangleName=elementName.ToString();在xaml.cs中,但我得到一个错误:我的xaml.cs不包含“ViewModel”的定义
<UserControl  x:Class="G.View.MapView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
      xmlns:extended="clr-namespace:G.ViewModel">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".7*" />
            <ColumnDefinition Width=".3*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height=".8*" />
            <RowDefinition Height=".2*" />
        </Grid.RowDefinitions>
        <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Grid.Column="0" Grid.Row="0" Margin="10,10,0,0">
            <ItemsControl ItemsSource="{Binding CaskList}" HorizontalAlignment="Left" VerticalAlignment="Top">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Width="1680" Height="800">
                            <Canvas.InputBindings>
                                <!-- One type of implementation -->
                                <KeyBinding Key="{Binding MouseLeftButtonDown.GestureKey}" Command="{Binding Path=HandleMyEvent}"/>
                            </Canvas.InputBindings>
                        </Canvas>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ContentPresenter">
                        <Setter Property="Canvas.Left" Value="{Binding Left}"/>
                        <Setter Property="Canvas.Top" Value="{Binding Top}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Rectangle Stroke="Black" Width="64" Height="64" Fill="{Binding Color}"></Rectangle>
                            <Label Content="{Binding ID}" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>      
    </Grid>
    </UserControl>
private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    string selectedRect;
    if (e.OriginalSource is Rectangle)
    {
        Rectangle ClickedRectangle = (Rectangle)e.OriginalSource;

        var mouseWasDownOn = e.Source as FrameworkElement;
        string elementName = mouseWasDownOn.Name;
        selectedRect = elementName.ToString();

        //ViewModel code
        var vm = this.ViewModel as YourViewModel;
        vm.RectangleName = elementName.ToString();
    }
}