locked
Grid delete button - code behind

    Question

  • Hi,

    I am new to this and am trying to wire up a delete button.  I have the below grid in a template where the grid is created each time depending on the number of items in the datasource (which selects the item name and the item ID).  I am trying to figure how I could pass what is the ID of the item selected for deleting to a delete command in the code behind. 

    Most examples I have found relate to mvvm which I do not want to use at the moment.  I presume I use the button command function but I would appreciate how to do this.

    Any tutorials links would also be greatly appreciated as this stuff is difficult to find.

    Thanks

    <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="100"  />
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="Auto" />
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Grid.Column="0" Foreground="{StaticResource GlyphButtonForeground}" FontWeight="Bold">
                                                <Run Text="Rates:" />
                                            </TextBlock>
                                            <Editors:TextEdit Grid.Column="1" Text="{Binding}"  ShowErrorToolTip="False">
                                            </Editors:TextEdit>
                                            <Button Grid.Column="2" Style="{StaticResource RemoveGlyphButton}"    Command="" />
                                        </Grid>

    Sunday, July 20, 2014 11:53 PM

Answers

  • Hi Ireland75,

    Once you get the Grid, its should not be difficult to get the child from it, in your situation the TextEditor Text is the id, and it is the second UIElement in the Grid template, we can use following code:

    var grid = (sender as Button).Parent as Grid;
    var texteditor = grid.Children[1] as TextEditor;
    var id = texteditor.Text;

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    Tuesday, July 22, 2014 3:07 AM
    Moderator

All replies

  • Hi Ireland75,

    You can always use UIElement.Parent to get its parent control.

    Let's say when you click the button, you will fire a Click event, and you could get the button by its argument "sender", (sender as Button).Parent should be your Grid, and (sender as Button).Parent as the parent control of the Grid, I assume it is another Grid, then we can use following code:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var grid = (sender as Button).Parent as Grid;
                var container = grid.Parent as Grid;
                container.Children.Remove(grid);
            }

    However MVVM is a better way to do this by modify the source backend.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, July 21, 2014 1:52 AM
    Moderator
  • Hi James,

    Thanks for that...I can now access the parent grid. 

    Could you clarify a further thing for me on how I would pass a variable to the click event.  Each grid has an 'ID' value and a 'Name' value pulled back from the datasource (in the code above the 'Name' value is bound to the texteditor).  I need to get the 'ID' to make a further update in the DB...so how could I get the 'ID' value of the specific grid passed to the click event?

    Thanks


    • Edited by Ireland75 Monday, July 21, 2014 5:06 PM clarify
    Monday, July 21, 2014 5:04 PM
  • Hi Ireland75,

    Once you get the Grid, its should not be difficult to get the child from it, in your situation the TextEditor Text is the id, and it is the second UIElement in the Grid template, we can use following code:

    var grid = (sender as Button).Parent as Grid;
    var texteditor = grid.Children[1] as TextEditor;
    var id = texteditor.Text;

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    Tuesday, July 22, 2014 3:07 AM
    Moderator
  • Thanks James..that explains clearly how it works..much appreciated.
    Wednesday, July 23, 2014 10:38 PM