views:

49

answers:

3

Photosuru has a neat effect - when you mouseover the thumbnail image a popup opens showing the image enlarged. I am trying to get this to work on a listbox, similar to a tooltip, I need to mouseover an item and have the popup open. The problem, the popup only shows the item selected in the listbox. I tried looking through Photosuru code for the answer, but found it too advanced for me. Note: I can't use tooltip as it is needed for something else.

Here's the xaml:

    <Window.Resources>

    <XmlDataProvider x:Key="MyPartsXML"

                         Source="F:\ListBoxSync\MyParts.xml"
                         XPath="MyParts"
        />

</Window.Resources>

<Grid x:Name="MainGrid" DataContext="{Binding ElementName=PartsList, Path=SelectedItem}" Width="Auto" VerticalAlignment="Stretch">

    <ListBox ItemsSource="{Binding Source={StaticResource MyPartsXML}, XPath=//MyParts//parts}" 
             IsSynchronizedWithCurrentItem="True" Name="PartsList" Foreground="Black" BorderBrush="Black" Width="115" HorizontalAlignment="Left">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="2" BorderBrush="Black" Margin="10">
                        <TextBlock Name="lstbxBlock" Text="{Binding XPath=item}" MouseEnter="item_MouseEnter" MouseLeave="item_MouseLeave" 
                        Width="75" FontSize="16" FontWeight="Bold" Padding="5" ToolTip="{Binding XPath=color}" />
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <Popup x:Name="Pops" IsOpen="False" Placement="Right" HorizontalOffset="6"  VerticalOffset="0" MinHeight="100" MinWidth="100"
           StaysOpen="False" AllowsTransparency="True" PlacementTarget="{Binding ElementName=txtBxitem}" Margin="2" MaxWidth="100" HorizontalAlignment="Left" Width="100">
            <StackPanel>
                   <TextBox Text="{Binding XPath=color}" FontSize="20" FontWeight="Bold" Background="LightBlue" />
                   <TextBox Text="{Binding XPath=size}" FontSize="20" FontWeight="Bold" Background="LightBlue" />

            </StackPanel>
    </Popup>

    <TextBox Text="{Binding XPath=color}" Margin="12,0,0,63" Height="30" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" />
    <TextBox Text="{Binding XPath=size}" Margin="12,0,0,27" Height="30" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" />

</Grid>

The code behind:

       private void item_MouseEnter(object sender, MouseEventArgs e)
    {
        this.Pops.IsOpen = true;
    }

    private void item_MouseLeave(object sender, MouseEventArgs e)
    {
        this.Pops.IsOpen = false;
    }

Hope this isn't overkill, but here's the xml:

 <?xml version="1.0" encoding="ISO-8859-1" ?> 

<MyParts>
<parts>
    <item>Part1</item>
    <color>Red</color>
    <size>SM</size>
</parts>
<parts>
    <item>Part2</item>
    <color>Green</color>
    <size>LG</size>
</parts>
<parts>
    <item>Part3</item>
    <color>Blue</color>
    <size>XXL</size>
</parts>
<parts>
    <item>Part4</item>
    <color>Yellow</color>
    <size>LG</size>
</parts>
<parts>
    <item>Part5</item>
    <color>Green</color>
    <size>XL</size>
</parts>

A: 
<html>
<title>CodeAve.com(JavaScript: Hover 
Window within Previous Page)</title>
<body bgcolor="#FFFFFF">

<script language="JavaScript">
<!-- 
// This is the function that will open the
// new window when the mouse is moved over the link
function open_new_window() 
{
new_window = open("","hoverwindow","width=300,height=200,left=10,top=10");

// open new document 
new_window.document.open();

// Text of the new document
// Replace your " with ' or \" or your document.write statements will fail
new_window.document.write("<html><title>JavaScript New Window</title>");
new_window.document.write("<body bgcolor=\"#FFFFFF\">");
new_window.document.write("This is a new html document created by JavaScript ");
new_window.document.write("statements contained in the previous document.");
new_window.document.write("<br>");
new_window.document.write("</body></html>");

// close the document
new_window.document.close(); 
}

// This is the function that will close the
// new window when the mouse is moved off the link
function close_window() 
{
new_window.close();
}

// -->
</script>


<a href="#" onMouseOver="open_new_window()" onMouseOut="close_window()">Open Hover Window</a>




</body>
</html>

Some code I found that completes this using simple Javascript. You could definitely incorporate this into .NET using your custom controls pretty easily. This shows how the functionality of the popup should be setup though.

Scott
Scott, I am working on a WPF desktop app. Your answer appears to me to be for a webpage.
Ken
Woops....my bad. I saw the question title and knew I looked at this a little while before so I posted it, haha.
Scott
A: 

First, thanks for your excellent sample code to reproduce your issue. The problem is that the data context of the popup is never set so it gets it from it parent, the grid which you set to currently selected item. In the code behind you can set the correct data contect of the popup.

private void item_MouseEnter(object sender, MouseEventArgs e)
{
   Pops.DataContext = (sender as FrameworkElement).DataContext;
   Pops.PlacementTarget = (sender as UIElement);
   Pops.IsOpen = true;
}

Also, you can't set the placement target like you do in xaml, it is not possible to just reference a control in a data template. The code behind fix will set placement target but hide your regular tooltip unless you add an offset to one of them. Personally I don't think its a good idea to have two popups on mouse over.

Wallstreet Programmer
A: 

I tried the answer offered by Wallstreet Programmer. It worked perfectly, solving the problem I was having. 2 lines of code - I'm impressed! Thank You

Lone Starr