wpf 整个工具提示的背景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13807354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backgroundcolor of entire ToolTip
提问by user1011394
Does anyone know a simple XAML solution to change the entire background of a ToolTip?
有谁知道一个简单的 XAML 解决方案来改变一个ToolTip?
I did the following:
我做了以下事情:
<Image Height="16" Source="Images/Icons/Add2.png" Stretch="Fill" Width="16" Opacity="0.99" Grid.Column="0">
<Image.ToolTip>
<Grid Background="#000000">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Header1" FontSize="15" Grid.Row="0"/>
<TextBlock Text="Subitem" FontSize="12" Grid.Row="1"/>
</Grid>
</Image.ToolTip>
</Image>
But the result looks like that:
但结果是这样的:


Any suggestions?
有什么建议?
回答by Tim
The problem is that all you're really doing is setting the CONTENT of the tooltip, not the tooltip itself.
问题是您真正要做的只是设置工具提示的内容,而不是工具提示本身。
So you'll need to style the tooltip to make this happen. There are some ways to do it with resources as seen in this post:
因此,您需要对工具提示进行样式设置以实现此目的。如本文所示,有一些方法可以使用资源来做到这一点:
WPF- Changing Tooltip background to Transparent
or you can change your code to wrap that grid with an explicit ToolTip and set its background property:
或者您可以更改代码以使用显式工具提示包装该网格并设置其背景属性:
<Image.ToolTip>
<ToolTip Background="Black">
<Grid>
...
</Grid>
</ToolTip>
</Image.ToolTip>
回答by Maghalakshmi Saravana
To change or Set the Background/Foreground color for the Tooltip in DataGrid in WPF
在 WPF 中更改或设置 DataGrid 中工具提示的背景/前景色
- Create the DataGrid as "Grid1" and add the DataGridTextColumn which contains the ToolTip with item.
- To change the Background colour for the ToolTip, add the Setter property in the DataGrid Resources
- 将 DataGrid 创建为“Grid1”并添加包含带有项目的工具提示的 DataGridTextColumn。
- 要更改工具提示的背景颜色,请在 DataGrid 资源中添加 Setter 属性
<DataGrid x:Name="Grid1">
<DataGrid.Resources>
<Style TargetType="ToolTip">
<Setter Property="Background" Value="Black"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
</Style>
</DataGrid.Resources>
<DataGridTextColumn Header="ActionName" Binding={Binding ActionName}>
<DataGridTextColumn.CellStyle>
<Setter Property="Control.ToolTip">
<Setter.Value>
<UniformGrid Columns="1">
<TextBlock Text="Action List" FontWeight="Bold"/>
<TextBlock Text="Cut"/>
<TextBlock Text="Copy"/>
<TextBlock Text="Delete"/>
</UniformGrid>
</Setter.Value>
</Setter>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid>
回答by kasper
To set the tooltip background you can override the style of the tooltip for the parent control. Below is your code with added style.
要设置工具提示背景,您可以覆盖父控件的工具提示样式。下面是添加了样式的代码。
<Image Height="16" Source="Images/Icons/Add2.png" Stretch="Fill" Width="16" Opacity="0.99" Grid.Column="0">
<Image.Resources>
<Style TargetType="ToolTip" BasedOn="{StaticResource {x:Type ToolTip}}">
<Setter Property="Background" Value="#000000" />
</Style>
</Image.Resources>
<Image.ToolTip>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Header1" FontSize="15" Grid.Row="0"/>
<TextBlock Text="Subitem" FontSize="12" Grid.Row="1"/>
</Grid>
</Image.ToolTip>
</Image>

