C# 如何以编程方式向按钮添加包含两行和一个内容的网格
我在一个包含图像和文本的XAML中创建了一个按钮,我在按钮中使用了网格。 以下是我的XAML代码:C# 如何以编程方式向按钮添加包含两行和一个内容的网格,c#,wpf,button,grid,styling,C#,Wpf,Button,Grid,Styling,我在一个包含图像和文本的XAML中创建了一个按钮,我在按钮中使用了网格。 以下是我的XAML代码: <Button x:Name="btnAddNewItem" Grid.Column="0" Grid.Row="0" FontSize="15" BorderThickness="1.5" HorizontalContentAlig
<Button x:Name="btnAddNewItem"
Grid.Column="0" Grid.Row="0"
FontSize="15"
BorderThickness="1.5"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Foreground="White"
Background="White"
BorderBrush="#0091EA" Margin="5,0,0,0" Height="90" Width="90">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Button.Template>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80*">
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<Image Source="/MyProject.Wpf;component/Icons/customer-icon.png" Margin="10" Grid.Row="0" Width="Auto"/>
<TextBlock Foreground="Black" Margin="0,0,0,3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" >Customer</TextBlock>
</Grid>
</Button>
如您所见,按钮中没有图像和文本,即使看起来像是我将它们添加到添加到按钮的网格中
所以我想我在按钮上添加网格时出错了
谢谢各位,
干杯
为教育署编辑:
switch (buttonPurpose)
{
case SettingsSubmenuItemEnum.Test:
{
button.HasIcon = true;
button.Icon = Controls.Enumerations.IconType.Other;
button.Click += EH_testButton_Click;
button.IconAlignHorizontal = HorizontalAlignment.Left;
button.TextAlignHorizontal = HorizontalAlignment.Right;
break;
}
case SettingsSubmenuItemEnum.Groups:
{
if (!OperatorController.HasOperatorAccess(currentOperator)
return;
button.HasIcon = true;
button.Icon = Controls.Enumerations.IconType.Grupe;
button.Click += EH_SubmenuButtonClickGrupePromet;
button.IconAlignHorizontal = HorizontalAlignment.Left;
button.TextAlignHorizontal = HorizontalAlignment.Right;
break;
}
case SettingsSubmenuItemEnum.Cities:
{
if (!OperatorController.HasOperatorPristup(currentOperator))
return;
button.HasIcon = true;
button.Icon = Controls.Enumerations.IconType.Other;
button.Click += EH_MjestaClick;
button.IconAlignHorizontal = HorizontalAlignment.Left;
button.TextAlignHorizontal = HorizontalAlignment.Right;
break;
}
spContentSubmenu.Children.Add(button);
}
}
在上面的代码中,我检查用户单击了哪个按钮,然后向他显示相应的按钮
变量buttonpurpose
是SettingsSubmenuItemEnum
类型
所以我想我在按钮上添加网格时出错了
您需要将图像和文本块添加到网格的子集合:
...
Grid.SetRow(imgControl, 0);
Grid.SetRow(text, 1);
//ADD:
grid1.Children.Add(imgControl);
grid1.Children.Add(text);
a.Content = grid1;
你为什么这么做?您有一个输入错误@EdPlunkett,因为我在屏幕顶部有一个菜单(现在不可见),该菜单实际上是堆栈面板中的5个按钮,每个按钮都有意义,例如,当您单击第一个名为Customer的按钮时,它应该生成4个新按钮,例如,添加新客户,检查客户角色,查看非活跃客户等。。这就是为什么我需要创建这种按钮的编程…使用一个模板。不要在这上面浪费时间。为什么需要以编程方式创建按钮?而且,每次我看到有人说“你给我”,我对帮助的兴趣就会下降50%。
...
Grid.SetRow(imgControl, 0);
Grid.SetRow(text, 1);
//ADD:
grid1.Children.Add(imgControl);
grid1.Children.Add(text);
a.Content = grid1;