如何将此.XAML代码转换为C#代码

时间:2018-12-28 15:45:41

标签: c# wpf xaml visual-studio-2017 code-translation

我需要一些帮助,将下面的.XAML代码转换为相应的C#代码:

<ContextMenu x:Name="MenuImageContextMenu" Background="White" Width="175" Height="100">
    <ContextMenu.Template>
        <ControlTemplate>
            <Grid x:Name="ContextMenuGrid" Background="{TemplateBinding Background}">
                  <Grid x:Name="BeverageGrid" Background="{TemplateBinding Background}" Height="50">
                      <Grid.ColumnDefinitions>
                          <ColumnDefinition Width="0.5*" />
                          <ColumnDefinition Width="3.5*" />
                          <ColumnDefinition Width="6*" />
                      </Grid.ColumnDefinitions>

                      <Image x:Name="BeverageImage" Grid.Column="1" Grid.RowSpan="3" Source="/DinerPOS;component/Resources/Images/Restaurant/Beverages/Beverage.png" Stretch="Fill" />

                      <TextBlock x:Name="BeverageLabel" Grid.Column="2" Grid.RowSpan="3" Text="Beverages" HorizontalAlignment="Center" TextAlignment="Center" VerticalAlignment="Center" />
                 </Grid>
            </Grid>
        </ControlTemplate>
    </ContextMenu.Template>
</ContextMenu>

到目前为止我尝试过的

ContextMenu ContextMenu = new ContextMenu();
ControlTemplate ControlTemplate = new ControlTemplate();

// ControlTemplate.VisualTree = Grid ????

ContextMenu.Name = MenuImageContextMenu;
ContextMenu.Template = ControlTemplate;

但是我不知道如何将主网格ContextMenuGrid添加到ControlTemplate

1 个答案:

答案 0 :(得分:0)

您可以使用XamlReader.Parse从XAML字符串创建ContextMenu元素:

const string Xaml = "<ContextMenu xmlns =\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
          "Background=\"White\" Width=\"175\" Height=\"100\"> " +
          "    <ContextMenu.Template> " +
          "        <ControlTemplate> " +
          "            <Grid x:Name=\"ContextMenuGrid\" Background=\"{TemplateBinding Background}\"> " +
          "                  <Grid x:Name=\"BeverageGrid\" Background=\"{TemplateBinding Background}\" Height=\"50\"> " +
          "                      <Grid.ColumnDefinitions> " +
          "                          <ColumnDefinition Width=\"0.5*\" /> " +
          "                          <ColumnDefinition Width=\"3.5*\" /> " +
          "                          <ColumnDefinition Width=\"6*\" /> " +
          "                      </Grid.ColumnDefinitions> " +
          "                      <Image Grid.Column=\"1\" Grid.RowSpan=\"3\" Source=\"/DinerPOS;component/Resources/Images/Restaurant/Beverages/Beverage.png\" Stretch=\"Fill\" /> " +
          "                      <TextBlock Grid.Column=\"2\" Grid.RowSpan=\"3\" Text=\"Beverages\" HorizontalAlignment=\"Center\" TextAlignment=\"Center\" VerticalAlignment=\"Center\" /> " +
          "                 </Grid> " +
          "            </Grid> " +
          "        </ControlTemplate> " +
          "    </ContextMenu.Template> " +
          "</ContextMenu>";
ContextMenu ct = XamlReader.Parse(Xaml) as ContextMenu;