2015-11-21 42 views
0

如何獲取窗口的XAML代碼作爲字符串,更改此字符串並將此字符串作爲窗口的xaml代碼上傳?在WPF中獲取XAML代碼

例如,我有

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ComboBox Margin="0,0,9,38" Name="button1" Height="82" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="132"></ComboBox> 
    </Grid> 
</Window> 

,我想如果你使用WPF/XAML與C#有這個XAML代碼到底

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ComboBox Margin="0,0,9,38" Name="button1" Height="82" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="132"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <local:d_dddw_imp_insurance_list /> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
     </ComboBox> 
    </Grid> 
</Window> 
+2

很確定窗口的XAML不可用,因爲它獲取_compiled_。如果你想改變你的_window_,只需使用WPF API。沒有必要在運行時擺弄XAML – MickyD

+0

上傳是什麼意思? – Default

+1

更改事情在運行時顯示的方式不是通過將xaml修改爲文本來完成的。你真的想實現什麼? –

回答

3

,視覺工作室通常編譯XAML代碼到C#,儘可能保持高性能。如果你想擁有一些動態的XAML,你需要使用XamlReader來讀取xaml並在「自己的」上顯示。

只要看一看:Loading XAML XML through runtime

比你可以很容易改變或操縱XAML和顯示結果。