0

我有 XAML 字符串,如:

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

如何将所有大括号转换为代码中的元素,例如:

    <Button>
      <Button.Content>
         <Binding Path="Name">
      </Button.Content>
      <Button.Style>
         <StaticResource ResourceKey="ButtonStyle"/>
      </Button.Style>
    </Button>
4

1 回答 1

2

您可以使用XamlReader并且XamlWriter我认为(!)默认情况下不会保存速记品种。请参阅http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader(v=vs.110).aspx

其中包括一个例子

// 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);

上面的保存按钮看起来像

 <Button Background="#FFF0F8FF" Width="100" Height="50" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">Click Me</Button>
于 2014-02-26T09:11:30.980 回答