Skip to main content

Microsoft Silverlight

Answered Question datagrid focus & MouseLeftButtonDownRSS Feed

(0)

oneone
oneone

Member

Member

15 points

31 Posts

datagrid focus & MouseLeftButtonDown

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_pal1979
amit_pal...

Participant

Participant

1150 points

201 Posts

Re: datagrid focus & MouseLeftButtonDown

Can you show me the sample code for Button 2?

oneone
oneone

Member

Member

15 points

31 Posts

Re: Re: datagrid focus &amp; MouseLeftButtonDown

 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 Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: Re: datagrid focus &amp; MouseLeftButtonDown

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

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities