none
Clearing a specified list of UI objects from layout in C# RRS feed

  • Question

  • Hi, 

     I'm trying to set up a modular arrangement of buttons and textblocks which can be deleted using on one of the buttons within, I can't seem to find the keywords to make it happen.

    My list and all the listed items are in bold, the event handler is in bold and italic at the bottom:

     ICollection<UIElement> firstOpening = new List<UIElement>();
    
                    Button duplicateOpening = new Button();
                    duplicateOpening.Margin = margin2;
                    duplicateOpening.Style = style2;
                    duplicateOpening.Content = "ADD";
                    duplicateOpening.Click += duplicateOpening_Click;
                    firstOpening.Add(duplicateOpening);
                    RSub.Children.Add(duplicateOpening);
    
                    TextBlock openingCode = new TextBlock();
                    openingCode.Margin = margin1;
                    openingCode.Text = OpeningCode();
                    openingCode.Style = style1;
                    firstOpening.Add(openingCode);
                    RSub.Children.Add(openingCode);
    
                    Button deleteOpening = new Button();
                    deleteOpening.Margin = margin2;
                    deleteOpening.Style = style2;
                    deleteOpening.Content = "DEL";
                    deleteOpening.Click += deleteOpening_Click;
                    firstOpening.Add(deleteOpening);
                    RSub.Children.Add(deleteOpening);
    
                    TextBlock openingDescription = new TextBlock();
                    openingDescription.Margin = margin1;
                    openingDescription.Text = OpeningSpecs();
                    openingDescription.Style = style1;
                    firstOpening.Add(openingDescription);
                    RSub.Children.Add(openingDescription);
    
                    void deleteOpening_Click(object sender1, RoutedEventArgs e1)
                    {
                        RSub.Children.Remove(firstOpening);
                    }

    I guess the reference to what i want to remove must be close to what i've written, but i get a red line under 'firstOpening' within 'RSub.Children.Remove(firstOpening);'

    Any advice would be apprecitated,

    Cheers


    • Edited by matty1248 Sunday, February 10, 2019 6:26 PM
    Sunday, February 10, 2019 5:11 PM

Answers

  • Hello,

    RSub is a panel, right? The key point here is that Panel.Children.Remove only support a UIElement as a item:

    Panel.Children.Remove(UIElement item)

    It does not contain any overload to remove the collection.

    So you have to create a loop to loop your collection and remove them one by one in your loop.  Like the following code:

    ICollection<UIElement> firstOpening = new List<UIElement>();
                foreach(var item in firstOpening)
                {
                    RSub.Children.Remove(item);
                }

    Best regards,

    Barry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by matty1248 Monday, February 11, 2019 8:05 PM
    Monday, February 11, 2019 7:09 AM
    Moderator

All replies

  • Hello,

    RSub is a panel, right? The key point here is that Panel.Children.Remove only support a UIElement as a item:

    Panel.Children.Remove(UIElement item)

    It does not contain any overload to remove the collection.

    So you have to create a loop to loop your collection and remove them one by one in your loop.  Like the following code:

    ICollection<UIElement> firstOpening = new List<UIElement>();
                foreach(var item in firstOpening)
                {
                    RSub.Children.Remove(item);
                }

    Best regards,

    Barry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by matty1248 Monday, February 11, 2019 8:05 PM
    Monday, February 11, 2019 7:09 AM
    Moderator
  • That's great, Thanks for that Barry, problem solved
    Monday, February 11, 2019 8:06 PM