获取WPF中单击矩形的名称

本文关键字:WPF 单击 获取 | 更新日期: 2023-09-27 17:59:42

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

<Canvas Width="1680" Height="800" MouseLeftButtonDown="canvas_MouseLeftButtonDown">

XAML.cs

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();
    }
}

它工作得很好,但我不知道如何将selectedRect字符串传递给我的ViewModel因此,我尝试将Relay命令从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

获取WPF中单击矩形的名称

试试这个:

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();
    }
}