[C#/WPF] XAML 구하기

C#/WPF 2019.09.01 13:11

■ XAML 구하기 예제

------------------------------------------------------------------------------------------------------------------------

using System.Windows.Controls;

 

TextBox textBox = new TextBox();

 

textBox.Name = "textBox";

 

string xaml = GetXAML(textBox);

------------------------------------------------------------------------------------------------------------------------

 

■ XAML 구하기

------------------------------------------------------------------------------------------------------------------------

using System.Text;

using System.Windows.Markup;

using System.Xml;

 

#region XAML 구하기 - GetXAML(sourceObject)

 

/// <summary>

/// XAML 구하기

/// </summary>

/// <param name="sourceObject">소스 객체</param>

/// <returns>XAML</returns>

public string GetXAML(object sourceObject)

{

    XmlWriterSettings settings = new XmlWriterSettings();

 

    settings.Indent              = true;

    settings.IndentChars         = new string(' ', 4);

    settings.NewLineOnAttributes = true;

    settings.OmitXmlDeclaration  = true;

 

    StringBuilder stringBuilder = new StringBuilder();

 

    XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings);

 

    XamlWriter.Save(sourceObject, xmlWriter);

 

    return stringBuilder.ToString();

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요