locked
Displaying Chm content inside WPF control RRS feed

  • Question

  • Hi all,

    I have Chm file for context menu that is currently working fine now.
    But I need the WPF control tooltip to also display a html file from the Chm based on a keyword specific to that control.

    Is there a way to do that?

    The only way I can think of is by using an IE web browser within the control's tooltip and pass ms-help\ url to the browser.
    Is there any other way to achieve that?

    many thanks.
    Dikhi
    • Edited by Dikhi Thursday, August 14, 2008 5:06 AM clarify some sentence
    Thursday, August 14, 2008 5:04 AM

Answers

  • -> The only way I can think of is by using an IE web browser within the control's tooltip and pass ms-help\ url to the browser.
    Is there any other way to achieve that?

    I don't think there is any native WPF way of doing this, hosting the WPF's Frame or Browser control is a way to implement this type of feature, one caveat here is that ToolTip is hosted inside a Popup which has AllowsTransparency set to true to enable layered windows for it, layered windows doesn't support child windows, so you need to set this property to false when hosting child hwnd such as Frame and WebBrowser inside it.

    Hope this helps
    • Marked as answer by Marco Zhou Wednesday, August 20, 2008 10:49 AM
    Tuesday, August 19, 2008 8:43 AM
  • Well, the following code shows how to host WebBrowser control inside ToolTip:

    <Window x:Class="WpfTestHarness.WebBrowserInToolTip"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:WpfTestHarness"
        Title="WebBrowserInToolTip" Height="300" Width="300">
      <Button >
        <Button.ToolTip>
          <cc:MyToolTip StaysOpen="True" x:Name="toolTip">
            <WebBrowser Width="300" Height="200" Source="http://www.live.com" />
          </cc:MyToolTip>
        </Button.ToolTip>
      </Button>
    </Window>

    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;
        }
    }

    Hope this helps
    • Marked as answer by Dikhi Tuesday, August 26, 2008 2:33 AM
    Monday, August 25, 2008 9:29 AM

All replies

  • -> The only way I can think of is by using an IE web browser within the control's tooltip and pass ms-help\ url to the browser.
    Is there any other way to achieve that?

    I don't think there is any native WPF way of doing this, hosting the WPF's Frame or Browser control is a way to implement this type of feature, one caveat here is that ToolTip is hosted inside a Popup which has AllowsTransparency set to true to enable layered windows for it, layered windows doesn't support child windows, so you need to set this property to false when hosting child hwnd such as Frame and WebBrowser inside it.

    Hope this helps
    • Marked as answer by Marco Zhou Wednesday, August 20, 2008 10:49 AM
    Tuesday, August 19, 2008 8:43 AM
  • Hi Marco,

    Thanks for the tips. I can host the WebBrowser (which is a HwndHost) inside the PopUp that's great!
    Now, I need the popup to display with the tooltip, and made the code below. But I got the following error from the HwndHost: "BuildWindowCore failed to return the hosted child window handle."
    The error occured when I move my mouse back and forth between the textbox and the button.

    Did I put the browser correctly in the visual tree, or should I handle the pop up and its location manually (separate control than the Tooltip) ?

    Many thanks, Marco.
    Dikhi

    <TextBox Text="ABC" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />

    <Button>
      <Button.ToolTip>
        <ToolTip StaysOpen="True" ToolTipOpening="ToolTip_ToolTipOpening" >
            <Popup Placement="Relative" 
                   AllowsTransparency="False"
                   IsOpen="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToolTip}}, Path=IsOpen}" 
                   StaysOpen="True" 
                   PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToolTip}}}"
            >
                <WebBrowser Name="webBrowser1" Width="300" Height="200" Source="mk:@MSITStore:D:\HelpFile.CHM::/main.htm" />
            </Popup>
        </ToolTip>
      </Button.ToolTip>
    </Button>

    Thursday, August 21, 2008 10:49 AM
  • Well, the following code shows how to host WebBrowser control inside ToolTip:

    <Window x:Class="WpfTestHarness.WebBrowserInToolTip"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:WpfTestHarness"
        Title="WebBrowserInToolTip" Height="300" Width="300">
      <Button >
        <Button.ToolTip>
          <cc:MyToolTip StaysOpen="True" x:Name="toolTip">
            <WebBrowser Width="300" Height="200" Source="http://www.live.com" />
          </cc:MyToolTip>
        </Button.ToolTip>
      </Button>
    </Window>

    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;
        }
    }

    Hope this helps
    • Marked as answer by Dikhi Tuesday, August 26, 2008 2:33 AM
    Monday, August 25, 2008 9:29 AM
  • That's working, Zhou..

    Thanks a lot!

    Dikhi
    Friday, October 10, 2008 9:34 AM