XAML | When used XamlReader.Parse, not able to refer the items using the LogicalTreeHelper/VisualTre

Posted by Roopesh on Stack Overflow See other posts from Stack Overflow or by Roopesh
Published on 2010-06-10T10:37:00Z Indexed on 2010/06/10 10:42 UTC
Read the original article Hit count: 1002

Filed under:
|

Hi,

I am setting the dynamic xaml (I am reading the xaml from the DB) for the content of a tab using the below statement.

Tab.Content = XamlReader.Parse(xaml, ctx)

After setting the content, if I try getting the children using the VisualTreeHelper, but I am not able to get. How ever I dont have this issue when I construct the xaml statically.

Here is the code to reading the xaml.

    Dim XmlDocument = New XmlDataDocument()
    Dim IID As String = Nothing
    Dim xaml As String = Nothing
    Dim Tab As New TabItem
    Dim TempPanel As XmlNode = Nothing
    'Tab.Height = 0
    Try
        XmlDocument.Load(Directory.GetCurrentDirectory & "\Xml\AppFile.xml")
        pXmlDoc = XmlDocument
        xaml = XmlDocument.SelectSingleNode("//Grid").OuterXml
        Dim AsmName As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
        Dim ctx As ParserContext = New ParserContext() ' New ParserContext()
        ctx.XamlTypeMapper = New XamlTypeMapper(New String() {AsmName})
        ctx.XamlTypeMapper.AddMappingProcessingInstruction("src", "WpfToolkitDataGridTester", AsmName)
        ctx.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
        ctx.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml")
        ctx.XmlnsDictionary.Add("src", "clr-namespace:WpfToolkitDataGridTester;assembly=" + AsmName)

        Tab.Name = "Tab" & Grid1Tab.Items.Count + 1
        Tab.Header = "AppFile-1"
        Tab.BorderThickness = New Thickness(0)
        Tab.IsSelected = True
        Tab.Content = XamlReader.Parse(xaml, ctx)
        Grid1Tab.Items.Add(Tab)
        Return True
    Catch ex As Exception
        Throw
    End Try

Here is the code to access the item after constructing the XAML.

        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(myVisual) - 1
            Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(myVisual, i), Visual)
            Select Case childVisual.DependencyObjectType.Name
                Case "ComboBox"
                    AddHandler CType(childVisual, ComboBox).SelectionChanged, AddressOf ComboBox_SelectChanged
                Case "CheckBox"
                    AddHandler CType(childVisual, CheckBox).Checked, AddressOf CheckBoxClicked
                    AddHandler CType(childVisual, CheckBox).Unchecked, AddressOf CheckBoxClicked
                Case "RadioButton"
                    AddHandler CType(childVisual, RadioButton).Checked, AddressOf CheckBoxClicked
                Case "TabControl"
                    For Each item As System.Windows.Controls.TabItem In CType(childVisual, TabControl).Items
                        EnumVisual(item.Content)
                    Next
            End Select
            EnumVisual(childVisual)
        Next i

any help is highly appreciated. Thanks,

© Stack Overflow or respective owner

Related posts about xaml

Related posts about xamlreader