How can I add XAML code using VB.Net? RRS feed

  • Question

  • How can I add XAML code or change one of the values in a XAML file using VB.Net?
    Tuesday, October 28, 2008 7:05 PM

All replies

  • Most of the .NET Framework 3.5 MSDN documentation includes samples in XAML, Visual Basic .NET and C#.  For example, check out the page on UIElement.MouseEnter Event.  This makes it easy to see how XAML interacts with both Visual Basic .NET and C#.

    Taking the sample code from the above link:

      <Border MouseEnter="OnMouseEnterHandler" 
              Name="border1" Margin="10" 
              Width="300" Height="100">  
        <Label Margin="10" FontSize="14" 
               HorizontalAlignment="Center">Move Cursor Over Me</Label> 

    Visual Basic
    Partial Public Class Window1  
        Inherits Window  
        Public Sub New()  
        End Sub 
        ' raised when mouse cursor enters the are occupied by the element  
        Private Sub OnMouseEnterHandler(ByVal sender As ObjectByVal e As MouseEventArgs)  
            border1.Background = Brushes.Red  
        End Sub 
        ' raised when mouse cursor leaves the are occupied by the element  
        Private Sub OnMouseLeaveHandler(ByVal sender As ObjectByVal e As MouseEventArgs)  
            border1.Background = Brushes.White  
        End Sub 
    End Class 

    public partial class Window1 : Window  
        public Window1()  
        // raised when mouse cursor enters the area occupied by the element  
        void OnMouseEnterHandler(object sender, MouseEventArgs e)  
            border1.Background = Brushes.Red;  
        // raised when mouse cursor leaves the area occupied by the element  
        void OnMouseLeaveHandler(object sender, MouseEventArgs e)  
            border1.Background = Brushes.White;  

    Jonathan - MSFT

    This posting is provided "AS IS" with no warranties and confers no rights
    Tuesday, October 28, 2008 10:44 PM
  • Hi Jonatha,
                     Thanks for the reply. I was hoping to find out how I could just write xaml as a string in a variable then just append it at a specified position within a xaml file.

    E.g. How could I add:

    <Button Margin="235,27,264,0" VerticalAlignment="Top" Height="65" Content="Button"/>

    to a xaml file from that same xaml file?

    Wednesday, October 29, 2008 1:09 PM
  • Hi QuddusAli,
                        Did you get anywhere with this? - I would also like to know how you do this.

    Friday, May 22, 2009 2:33 PM
  • Is this what you are trying to do?

    <Window x:Class="WpfApplication1.Window1"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
            <ContentControl Name="contentHolder"/>

    using System.Windows;
    using System.IO;
    using System.Xml;
    namespace WpfApplication1
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
            public Window1()
            private void Window_Loaded(object sender, RoutedEventArgs e)
                string strPrefix = "<ContentControl xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">";
                string strXAML = "<Button Height=\"50\" Width=\"100\" Content=\"Button\"/>";
                string strPostfix = "</ContentControl>";
                StringReader rd = new StringReader(strPrefix + strXAML + strPostfix);
                XmlTextReader rdX = new XmlTextReader(rd);
                contentHolder.Content = System.Windows.Markup.XamlReader.Load(rdX);

    • Proposed as answer by anthonyrsc Friday, June 5, 2009 9:57 AM
    Thursday, June 4, 2009 11:49 PM