如何将此 .XAML 代码翻译成 C# 代码
How can I translate this .XAML code into C# code
我需要一些帮助将下面的 .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
。
您可以使用 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;
我需要一些帮助将下面的 .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
。
您可以使用 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;