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

标签 c# wpf xaml mvvm

我正在尝试在WPF应用程序中获取单击的Rectangle的名称。
我首先尝试了这个:
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>

它可以正常工作,单击 Canvas 时我可以在方法中正确输入,但是无法获取矩形名称,没有事件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();
    }
}

关于c# - 获取WPF中单击的矩形的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31030748/

相关文章:

c# - 列表框可以显示字典中的键和值

c# - 在 WPF 的 ContentControl 中托管 ViewModel

c# - 如何使工具提示具有圆角 - WPF

c# - 动态更改 gridview 列的标题文本

C# 向组合框添加长字符串

c# - MVVM跟踪 View 模型中的问题

c# - 替换本地 : keyword with a path? 的适当方法是什么

c# - Visual Studio 2015 Update 2 Web 应用程序(表单)编辑并继续不起作用

c# - 如何在静态代码分析器实用程序中找到可能出现 NullReferenceException 的点?

c# - cefSharp ChromiumWebBrowser 大小到页面内容