如何从代码中添加带有工具提示、透明背景和字形的按钮?
How to add a button with Tooltip, Transparent background and Glyph from code?
在XAML中,您可以添加这样的按钮:
<Button Height="Auto" Width="Auto" Background="Transparent" ToolTipServie.ToolTip="Tooltip here" ToolTipService.Placement="Bottom" />
但是我无法用 C# 实现类似的方法。假设我有一个名为 "toolbar":
的 Stackpanel
toolbar.Children.Add(new Button {
Name = "undo",
FontFamily = new FontFamily("Segoe MDL2 Assets"),
Content = "",
Opacity = 100,
});
当我调试代码时,它添加了一个带有正方形、灰色背景的按钮。我也不知道如何给按钮添加工具提示。
我尝试寻求帮助,但没有任何帮助。 :(
非常感谢任何帮助。谢谢
工具提示不是普通的 DP,它是通过 ToolTipService class 附加的 属性,因此您需要这样添加它:
var button = new Button
{
Name = "undo",
FontFamily = new FontFamily("Segoe MDL2 Assets"),
Content = "",
Opacity = 100,
};
ToolTipService.SetToolTip(button, "Tooltip here");
ToolTipService.SetPlacement(button, PlacementMode.Bottom);
toolbar.Children.Add(button);
在XAML中,您可以添加这样的按钮:
<Button Height="Auto" Width="Auto" Background="Transparent" ToolTipServie.ToolTip="Tooltip here" ToolTipService.Placement="Bottom" />
但是我无法用 C# 实现类似的方法。假设我有一个名为 "toolbar":
的 Stackpaneltoolbar.Children.Add(new Button {
Name = "undo",
FontFamily = new FontFamily("Segoe MDL2 Assets"),
Content = "",
Opacity = 100,
});
当我调试代码时,它添加了一个带有正方形、灰色背景的按钮。我也不知道如何给按钮添加工具提示。
我尝试寻求帮助,但没有任何帮助。 :(
非常感谢任何帮助。谢谢
工具提示不是普通的 DP,它是通过 ToolTipService class 附加的 属性,因此您需要这样添加它:
var button = new Button
{
Name = "undo",
FontFamily = new FontFamily("Segoe MDL2 Assets"),
Content = "",
Opacity = 100,
};
ToolTipService.SetToolTip(button, "Tooltip here");
ToolTipService.SetPlacement(button, PlacementMode.Bottom);
toolbar.Children.Add(button);