user1139683
user1139683

Reputation: 3

Grid.GetRow() always returns 0

I am having trouble getting the row of a WPF grid that a textbox is in. I have a grid that starts off with one RowDefinition. That row contains an "add" button that adds another rowdefinition to the grid below that row. This new row also contains an "add" button that performs the same function.

The problem I am having is that the function GetRow() always returns 0.

If I declare a button in the XAML that calls the same function, GetRow() returns the correct value. The problem seems to stem from the face that the buttons are created in codebehind.

This is the function that handles the click event of the "add" buttons:

Private Sub btnAddRow_Click(ByVal sender As System.Object, _
  ByVal e As System.Windows.RoutedEventArgs)
  Dim btnSender As Button = sender
  Dim row As Integer
  row = Grid.GetRow(btnSender)
  AddRow(row)
End Sub

The function "AddRow" adds a new RowDefinition to the grid, the "add" button for that row, and a few other controls (label, textbox, etc).

Private Sub AddRow(ByVal position As Integer)
    Dim rd As New RowDefinition()
    rd.Height = New GridLength(35, GridUnitType.Pixel)
    Me.Height += 35
    myGrid.RowDefinitions.Insert(position, rd)
    Dim add As New Button
    add.Content = "Add Row"
    add.HorizontalAlignment = Windows.HorizontalAlignment.Center
    add.VerticalAlignment = Windows.VerticalAlignment.Center
    AddHandler add.Click, AddressOf btnAddRow_Click
    Grid.SetColumn(add, 2)
    Grid.SetRow(add, position)
    myGrid.Children.Add(add)
End Sub

I found this thread, but using "e.Source" or "e.OriginalSource" did not solve the problem. Grid.GetRow and Grid.GetColumn keep returning 0

EDIT:

Here is my code. I pulled it out of the project it was in and created a new project for testing.

Class MainWindow 
Private Sub btnAddRow_Click(ByVal sender As System.Object, _
                            ByVal e As System.Windows.RoutedEventArgs)
    Dim btnSender As Button = sender
    Dim row As Integer
    row = Grid.GetRow(btnSender)
    row = row + 1
    AddRow(row)
End Sub

Private Sub AddRow(ByVal position As Integer)
    If (myGrid.RowDefinitions.Count < position) Then
        position = myGrid.RowDefinitions.Count
    End If

For Each element In (From i As UIElement In myWaypointGrid.Children Where Grid.GetRow(i) >= position Select i).ToList()
            Grid.SetRow(element, Grid.GetRow(element) + 1)
        Next

    Dim rd As New RowDefinition()
    rd.Height = New GridLength(35, GridUnitType.Pixel)
    Me.Height += 35
    myGrid.RowDefinitions.Insert(position, rd)
    Dim add As New Button
    add.Content = "Add Row " & position
    add.HorizontalAlignment = Windows.HorizontalAlignment.Center
    add.VerticalAlignment = Windows.VerticalAlignment.Center
    AddHandler add.Click, AddressOf btnAddRow_Click
    Grid.SetColumn(add, 2)
    Grid.SetRow(add, position)
    myGrid.Children.Add(add)
End Sub

Private Sub MainWindow_Loaded(ByVal sender As Object, _
                              ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    AddRow(0)
End Sub

End Class

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="myGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="75" />
    </Grid.ColumnDefinitions>
</Grid>

Thanks for your help.

Upvotes: 0

Views: 993

Answers (1)

evasilchenko
evasilchenko

Reputation: 1870

Are you ever calling the AddRow function prior to the first "Add" button click? Without more code, it's hard to say why this is not working.

Update to reflect the true issue:

You don't do an increment on the position variable which gets passed into this function so all your buttons are being added to row 0. That is why they all return 0 when you call GetRow

Upvotes: 1

Related Questions