How can I translate this .XAML code into C# code

soulblazer

I need some help translating the following .XAML code below to its corresponding C# code:

<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>

What I have tried so far

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

// ControlTemplate.VisualTree = Grid ????

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

But I don't know how to add the main Grid ContextMenuGrid to the ControlTemplate.

mm8

You could use the XamlReader.Parse to create a ContextMenu element from a XAML string:

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I translate this PHP into C# code?

How can I translate this code from objective-C to Swift?

How can I translate the javascript code to clojurescript?

How can i translate this PHP code to C# asp.net-mvc?

How can I translate If-Else code to Python?

How can I translate this Perl code for RSA Public key in python?

How can I translate this piece of code from Erlang to Elixir?

How to translate Assembly code to C code?

How can I add a label in C# to a grid in my XAML code?

Adding a C# template to XAML code. How can I do the binding?

How can i insert a string generated in c# code into xaml button?

How can I translate C code that copies the length of an integer value followed by a byte string consisting of the value into arbitrary bytes?

how do I translate this javascript code

What can I do to not repeat code in xaml?

How to properly translate C code to UnsafeSwift

How to translate this portion of C++ code to Delphi

How to translate Morse Code to English in C++?

How would I translate this JavaScript code to Python code?

How can I read this C code?

How can I convert this code to C#?

How i can implement this Matlab code in C?

How do I generate XAML code programmatically?

How can I convert my obj C code to swift code

How can I reduce code repetition in this C code for PIC microcontroller?

How can I set "toXDelta" translate animation value from java code?

How can I translate this Kotlin code using zip() and lambda functions to Rust without the explicit "for" loops?

jQuery code need to translate to vanilla so I can use in Angular

How to code in c# programmatically (not using xaml)

how to create C# code for XAML Grid code?