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

时间:2015-06-24 15:28:33

标签: 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 所以我试图直接将来自xaml的Relay命令传递给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

1 个答案:

答案 0 :(得分:1)

试试这个:

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