Can XAML be modified via LINQ? - Community Credit Forums

    Community Credit Forums

Can XAML be modified via LINQ?

Last post Wed, Aug 20 2008 8:51 AM by KarlShifflett. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • Tue, Aug 19 2008 12:21 PM

    • ArdMan
    • Top 200 Contributor
    • Joined on Tue, Aug 19 2008
    • Posts 1

    Can XAML be modified via LINQ?

    XAML: A canvas, I have a few buttons and rectangles.

    Challenge: Remove the rectangles from the Canvas using only their types, but leaving the existing buttons alone.

    Thoughts: I've wondered if this is possible with LINQ, but I'm stupid green to LINQ.  I have seen LINQ used to query an SQL database and XML, but I've never seen an example of it tweaking XAML objects.  I'm thinking it's theorically possible.


    I've discovered:

    1. I can loop through all of the elements in a Canvas by referencing its "Children". 

    2. I can use the "GetType" method to find an element's type.

    3. Calling the "Remove" method of a Canvas inside of a foreach loop throws the program into an error.

    Filed under: , , ,
  • Wed, Aug 20 2008 8:51 AM In reply to

    Re: Can XAML be modified via LINQ?

    You can't remove items from a collection when you are iterating the collection.  Instead, make a list of the items references, then iterate from the new list and remove them. 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        Debug.WriteLine("Before: " & Me.cnvRoot.Children.Count)

        Dim objRemoveList As New List(Of Rectangle)

        For Each objRectangle As Rectangle In From r In Me.cnvRoot.Children Where TypeOf r Is Rectangle Select CType(r, Rectangle)



        For Each objRectangle In objRemoveList




        objRemoveList = Nothing

        Debug.WriteLine("After: " & Me.cnvRoot.Children.Count)

    End Sub



    Code Project MVP, Karl's Blog : Get Mole v4 Here also!

    Just a grain of sand on the worlds beaches.
Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems