如何在wpf c#中绑定文本+图像工具提示

时间:2022-09-13 09:39:54

I have a wpf program in c# that has a combobox with various colors to choose from. It looks something like this

我在c#中有一个wpf程序,它有一个可以选择各种颜色的组合框。它看起来像这样

? Text

I am making a configurator that can change the color of certain texts in another program. So if the user opens the program and he doesn't like the description text color, he can open up the configurator and change the color. When the user chooses a color from the combobox the Text changes color. When the user hovers his mouse over the ? an image appears showing an image of the program with the changed text color. So far I can make the image of the program appear, but I can't make any text appear on top of it. This is what I have right now:

我正在制作一个可以改变另一个程序中某些文本颜色的配置器。因此,如果用户打开程序并且他不喜欢描述文本颜色,他可以打开配置器并更改颜色。当用户从组合框中选择颜色时,文本会改变颜色。当用户将鼠标悬停在?出现一个图像,显示带有更改文本颜色的程序图像。到目前为止,我可以使程序的图像出现,但我不能使任何文本出现在它上面。这就是我现在所拥有的:

        System.Windows.Controls.Image td = new System.Windows.Controls.Image();
        BitmapImage myBitmapImage = new BitmapImage();
        myBitmapImage.BeginInit();
        myBitmapImage.UriSource = new Uri(Directory.GetCurrentDirectory() + "/Homepage.jpg");
        myBitmapImage.DecodePixelWidth = 700;
        myBitmapImage.DecodePixelHeight = 590;
        myBitmapImage.EndInit();
        td.Source = myBitmapImage;

        ToolTipService.SetPlacement(image1, System.Windows.Controls.Primitives.PlacementMode.Left);
        ToolTipService.SetToolTip(image1, td);
        ToolTipService.SetShowDuration(image1, 999999999);

How can I position text to appear on top of the ToolTip image?

如何将文本放置在ToolTip图像的顶部?

2 个解决方案

#1


ToolTipService allows you to set the tool tip to be any content. So set it as a panel containing both an image and text. The following code accomplishes this:

ToolTipService允许您将工具提示设置为任何内容。因此将其设置为包含图像和文本的面板。以下代码完成此操作:

TextBlock textBlock = new TextBlock();
textBlock.Foreground = Brushes.White;
textBlock.HorizontalAlignment = HorizontalAlignment.Center;
textBlock.VerticalAlignment = VerticalAlignment.Center;
textBlock.Text = "Overlaying Image Text";

Grid toolTipPanel = new Grid();
toolTipPanel.Children.Add(td);
toolTipPanel.Children.Add(textBlock);

ToolTipService.SetToolTip(image1, toolTipPanel);

#2


Try looking at the adorner layer. Basically the adorner layer allows you to display items without affecting the layout of the controls underneath.

尝试查看装饰图层。基本上,adorner图层允许您显示项目而不影响下面控件的布局。

#1


ToolTipService allows you to set the tool tip to be any content. So set it as a panel containing both an image and text. The following code accomplishes this:

ToolTipService允许您将工具提示设置为任何内容。因此将其设置为包含图像和文本的面板。以下代码完成此操作:

TextBlock textBlock = new TextBlock();
textBlock.Foreground = Brushes.White;
textBlock.HorizontalAlignment = HorizontalAlignment.Center;
textBlock.VerticalAlignment = VerticalAlignment.Center;
textBlock.Text = "Overlaying Image Text";

Grid toolTipPanel = new Grid();
toolTipPanel.Children.Add(td);
toolTipPanel.Children.Add(textBlock);

ToolTipService.SetToolTip(image1, toolTipPanel);

#2


Try looking at the adorner layer. Basically the adorner layer allows you to display items without affecting the layout of the controls underneath.

尝试查看装饰图层。基本上,adorner图层允许您显示项目而不影响下面控件的布局。