Skip to main content
Home Forums Silverlight Programming Programming with .NET - General datagrid focus & MouseLeftButtonDown
3 replies. Latest Post by Jonathan Shen – MSFT on October 2, 2008.
(0)
oneone
Member
15 points
31 Posts
09-29-2008 2:33 AM |
Hi, Every one : I have a datagrid like this -------------------------------- | | | --- | |value|value| | | <- button 1 | | | | --- | -------------------------------- | | | --- | |value|value| | | <- button 2 | | | | --- | -------------------------------- | | | --- | |value|value| | | <- button 3 | | | | --- | -------------------------------- | | | --- | I want when i press button 1, can get row1 detail, press button 2, get row2 detail. But now, when i press button 2, i still get row1 SelectItem. I try to search .... then i know i must to set the focus, and use MouseLeftButtonDown, but still not working >"< Can someone show me the sample code, thanks !!
amit_pal...
Participant
1150 points
201 Posts
10-01-2008 7:54 AM |
Can you show me the sample code for Button 2?
10-02-2008 3:57 AM |
thanks for your reply
i did nothing, just to get the row data, like this
void Button2ClickEvent( object sender, EventArgs e ){ object obj = List.DataGrid_OMUSABaseBallGame_A.SelectedItem;}
nomater i press buttn 1 or 2, always get row 1's data
Jonathan...
All-Star
24939 points
2,425 Posts
10-02-2008 6:55 AM |
Hi Oneone,
oneone: i did nothing, just to get the row data, like this void Button2ClickEvent( object sender, EventArgs e ){ object obj = List.DataGrid_OMUSABaseBallGame_A.SelectedItem;} nomater i press buttn 1 or 2, always get row 1's data
SelectedItem may not be the current item if the current selected row is not the row with the clicked Button. To get the current item, you shall do it like this.
void Page_Loaded(object sender, RoutedEventArgs e) { //myDataGrid.ItemsSource = Person.GetPersons(); //myListBox.ItemsSource = Person.GetPersons(); Data = Person.GetPersons(); this.DataContext = Data;
} ObservableCollection<Person> Data;
private void Button_Click(object sender, RoutedEventArgs e) { Button curButton = sender as Button; Person curPerson = curButton.DataContext as Person; //curPerson is the current item here. MessageBox.Show(Data.IndexOf(curPerson).ToString()); //myDataGrid.CurrentColumn.GetCellContent(curPerson); }
<data:DataGrid x:Name="myDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}"> <data:DataGrid.Columns> <data:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></data:DataGridTextColumn> <data:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"></data:DataGridTextColumn> <data:DataGridCheckBoxColumn Header="Male" Binding="{Binding Male}"></data:DataGridCheckBoxColumn> <data:DataGridTemplateColumn Header="Command"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Width="80" Height="25" Content="Click Me" Click="Button_Click"></Button> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid>
Best regards,
Jonathan