Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody,
I just want to get a clicked element name in the ViewModel code.
Basically, I generate several rectangles programmatically
XML
<ItemsControl ItemsSource="{Binding CaskList}" HorizontalAlignment="Left" VerticalAlignment="Top">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Width="1680" Height="800" MouseDown="Canvas_MouseDown">
                        </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>

I just want to get the name of the clicked rectangle in my ViewModel.
I tried in many ways: using "MouseLeftButtonUp" in <Canvas> works fine but I only get the name in the View code, then I am not able to pass it to the ViewModel (and AFAIK this is completly against MVVM rules). so I tried to use RelayCommand and it works fine in VM, but I don't have the event MouseButtonEventArgs anymore, so I don't know how to retrive the name of the clicked rectangle.

P.S. I was so frustrated that I tried to use buttons instead of Rectangles+Textbox but I don't know why the binding path doesn't work inside the canvas, and my ICommand was never called.

Please help me!
Posted
Comments
Member 11267379 1-Jul-15 3:04am    
if you need to follow the mvvm means, why not you try to use the behavior concept and use there mouseleftbuttonup event as same as in view.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900