0

I am trying to modify data in rows in a gridview which I have pulled from a database.

The data is being bound to each gridview column like the following.

<GridViewColumn Header="Status" Width="120" util:GridViewSort.PropertyName="Status">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Status}" FontWeight="Bold" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown_1"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

I have a MouseLeftButtonDown event on the textblock, and this fires when I click on the specific text.

private void TextBlock_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("Hello");
}

The issue I am having is that I can't find a way to access the row data(such as the id, or the text in the row).

Is there any way to access all the row data from within the click event?

Thanks

sangram parmar
  • 8,462
  • 2
  • 23
  • 47
DiscoDamo
  • 59
  • 7

2 Answers2

3

Try This:

  private void TextBlock_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
    {
        TextBlock textBlock = (sender as TextBlock);
        string text = textBlock.Text;// Text in the TextBlock
        object datacontext = textBlock.DataContext; // datacontext, Entire row info
    }
user2323308
  • 759
  • 5
  • 16
  • 34
0

You should not do anything not directly related to the UI in the code behind of your XAML file.. you should bind the click event to a command of your Model or ViewModel (the entity bound to your cell, in your case the entity which contains the "Status" property) ; that entity should have easier access to "the row data".

franssu
  • 2,422
  • 1
  • 20
  • 29
  • As a non native speaker I think I misunderstood row data for raw data :/ – franssu Sep 13 '13 at 10:52
  • Thanks for your help. My terminology probably isn't the best, I am very new to WPF. Basically I have 6 rows in a grid. All contain textblocks. when I click on one textblock, I want to get the text contained in the other textblocks in the same row. I can access the text of the block I click on using the sender object, but I cant find a way to get to the textblocks in the rest of the row. – DiscoDamo Sep 13 '13 at 10:57