在wpf工具提示中使用WindowsFormHost

本文关键字:WindowsFormHost wpf 工具提示 | 更新日期: 2023-09-27 18:04:51

我试图在ToolTip内托管一个windows窗体面板。下面是ToolTip的Xaml代码和后面的类。

问题是,如果我使用windowsFormsHost面板不改变颜色,感觉就像ToolTip甚至不知道它在那里。

我做的对吗?

(如果我能改变颜色,然后我将用它来显示一个摄像头的实时饲料)

当我点击按钮时,ToolTip在那里,但它保持基本。

如果我没有Windows窗体主机和使用StackPanel,那么它的工作原理。但是我需要使用Panel .

Xaml:

<Grid>
<Button Width="100" Height="100">
        <Button.ToolTip>
            <Controls:MyToolTip Height="500" Width="550">
                    <WindowsFormsHost x:Name="wrapper" Margin="0,0,0,0" Background="{x:Null}">
                        <wf:Panel x:Name="previewScreen" BackColor="Purple" Size="200,200" >
                        </wf:Panel>
                    </WindowsFormsHost>
            </Controls:MyToolTip>
        </Button.ToolTip>
  </Button>
  </Grid>
c#:

public class MyToolTip : ToolTip
{
    protected override void OnTemplateChanged(ControlTemplate oldTemplate, ControlTemplate newTemplate)
    {
        if (newTemplate != null)
        {
            this.Visibility = Visibility.Collapsed;
            this.IsOpen = true;
            Popup popup = GetPopupFromVisualChild(this);
            if (popup != null) popup.AllowsTransparency = false;
            this.IsOpen = false;
            this.Visibility = Visibility.Visible;
        }
    }
    private static Popup GetPopupFromVisualChild(Visual child)
    {
        Visual parent = child;
        FrameworkElement visualRoot = null;
        while (parent != null)
        {
            visualRoot = parent as FrameworkElement;
            parent = VisualTreeHelper.GetParent(parent) as Visual;
        }
        Popup popup = null;
        if (visualRoot != null)
        {
            popup = visualRoot.Parent as Popup;
        }
        return popup;
    }
}

感谢您的时间和帮助。

在wpf工具提示中使用WindowsFormHost

问题是面板没有内容,所以它不显示背景。

试试这个:

<Grid>
    <Button Width="100" Height="100">
        <Button.ToolTip>
            <Controls:MyToolTip >
                <WindowsFormsHost x:Name="wrapper" Margin="0,0,0,0" Background="{x:Null}" >
                    <wf:Panel x:Name="previewScreen" BackColor="Purple" Size="200,200" >
                        <wf:Panel.Controls>
                            <wf:Label Text="Test"></wf:Label>
                        </wf:Panel.Controls>                            
                    </wf:Panel>
                </WindowsFormsHost>
            </Controls:MyToolTip>
        </Button.ToolTip>
    </Button>
</Grid>