0

I have XAML string like:

    <Button Content={Binding Name} Style={StaticResource ButtonStyle}/>

How I can convert all braces to elements in code like:

    <Button>
      <Button.Content>
         <Binding Path="Name">
      </Button.Content>
      <Button.Style>
         <StaticResource ResourceKey="ButtonStyle"/>
      </Button.Style>
    </Button>
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
R.Titov
  • 3,115
  • 31
  • 35
  • It would have helped if you had explained what you are trying to achieve, so we knew why you want to do the conversion. – AlSki Feb 26 '14 at 09:11
  • I think it is an interesting topic, but can you show what you have tried so far to solve the problem yourself? The markup extension syntax is well-defined by Microsoft, so what is it in particular you are stuck at? – O. R. Mapper Feb 26 '14 at 09:18
  • By the way, your example illustrates the desired conversion, but it contains a few syntax errors. – O. R. Mapper Feb 26 '14 at 09:21
  • I need to create parser which read resource dictionary generated from Zam3D and change the xaml-file (change structure) to open it (as xml) in my program. The program serializing xml to native objects (not Controls or UIElements) – R.Titov Feb 26 '14 at 12:51
  • I need way to read the data as XmlElement or XElement but I can't do it correctly for data between braces. – R.Titov Feb 26 '14 at 12:59

1 Answers1

2

You can use XamlReader and XamlWriter which I think(!) by default does not save the shorthand variety. See http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader(v=vs.110).aspx

which includes an example

// Create the Button.
Button originalButton = new Button();
originalButton.Height = 50;
originalButton.Width = 100;
originalButton.Background = Brushes.AliceBlue;
originalButton.Content = "Click Me";

// Save the Button to a string. 
string savedButton = XamlWriter.Save(originalButton);

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);

saved button above looks like

 <Button Background="#FFF0F8FF" Width="100" Height="50" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">Click Me</Button>
AlSki
  • 6,868
  • 1
  • 26
  • 39