Get row selected index value in Mouse Left Button Down in Silverlight.

Posted by Arun Neon under Silverlight on 5/17/2012 | Points: 10 | Views : 3122 | Status : [Member] | Replies : 2
I have an image in a datagrid that shows below

<sdk:DataGridTemplateColumn x:Name="colStat" IsReadOnly="True" Header="Stat" Width="60" CanUserResize="False">
<StackPanel x:Name="spStats" VerticalAlignment="Center" Margin="10,0,0,0" Width="Auto" Height="Auto" HorizontalAlignment="Center" Orientation="Vertical" Cursor="Hand">
<Image x:Name="imgStats" Source="Assets/Images/stats.png" Height="25" Width="25"
Margin="0" MouseLeftButtonDown="imgStats_MouseLeftButtonDown" MouseLeftButtonUp="imgStats_MouseLeftButtonUp" />

I have two events as mentioned in my code MouseLeftButtonDown And MouseLeftButtonUp for the image,I can able to get my row selected index value in MouseLeftButtonUp but i couldn't able get selected index value in MouseLeftButtonDown. I need the Selected Index value in MouseLeftButtonDown also. Can anyone please help me solve this issue.



Posted by: Vuyiswamb on: 5/17/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

The Requirement is not clear for me

Thank you for posting at Dotnetfunda

Arun Neon, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Superb1986 on: 7/1/2012 [Member] Starter | Points: 25

Hi Arun,
You cant get the selected index of the datagrid on the mouseleftbutton down becoz it doesnt sets the selected item on that time, its a default behaviour of the datagrid so you can have some work around for this you can compare the selected items list to get the index from it. this will also work on paged collections view too. so use this below code.
 private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

int indexofList = dataGrid1.ItemsSource.Cast<DataObjects>().ToList().IndexOf((DataObjects)((System.Windows.FrameworkElement)(((System.Windows.RoutedEventArgs)(e)).OriginalSource)).DataContext);

for your reference i have attached the sample project you can go through that. let me know if you cant.
 Download source file

Arun Neon, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response