I have the following object:
public class Chip
{
public double PositionTop { get; set; }
public double PositionLeft { get; set; }
public double Size { get; set; }
public SolidColorBrush Fill { get; set; }
}
From the ViewModel I set an ObservableCollection Chips and in the view I have the following representation.
<ItemsControl ItemsSource="{Binding Chips}"
Grid.Row="0" Grid.RowSpan="20"
Grid.Column="0 " Grid.ColumnSpan="20">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<Ellipse Canvas.Left="{Binding PositionLeft}"
Canvas.Top="{Binding PositionTop}"
Fill="{Binding Fill}"
Style="{StaticResource Chip}"
Width="{Binding Size}"
Height="{Binding Size}"
Tapped="ViewModel.UIElement_OnTapped"/>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
When firing the Tapped event my intention is to change the position from the ViewModel, but I can't find out which list item is the one that has been clicked, since I can't make a direct "Cast" to the "Chip" object type. Any help or any clues to investigate?
Fixed.
I changed the Tapped event to the Canvas element and was able to Cast to the Chip object from the Canvas.DataContext.
The downside is that it doesn't update the position directly, so I remove the object from the collection and add a new object with the desired position. By the way, I take the opportunity to launch an animation from the initial position to the final position.
The event code looks like this: