Question

i am trying to host a windows form panel inside a ToolTip. Below is the Xaml code and the class behind for the ToolTip.

The issue is if I'm using windowsFormsHost the panel doesnt change the color, it feels like the ToolTip doesn't even know it's there.

Am I doing it right?

(If I'm able to change the color then I will use it to show a liveFeed of a camera)

When I click on the button, the ToolTip is there but it stays basic.

If I have no Windows Form Host and use a StackPanel then it works. But I need to use the 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;
    }
}

Thanks for your time and help.

Was it helpful?

Solution

The problem is that the panel has no content, so It doesn't show the background.

Try this:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top