tags:

views:

779

answers:

4

hi everyone. i am building an application. i have a button on a window. When i click the button, i want a new window popsup. but new window must open under button. As another way, when window popsup,button must been shown on window. How can i success that? is that possible? Thanks in advance.Lilly

My codes as follows,

private void Topics_Click(object sender, RoutedEventArgs e)
{


        TreeView tree = new TreeView();
        tree.Top = 250;
        tree.Left = 30;


       tree.Show();


}
A: 

Lilly,

Here is a class that will help you place any window relative to the control that you wish.

using System.Windows;
public static class WindowHelper
{
    public static void PlaceWindow(this Window window, FrameworkElement control)
    {
        if(window == null || control == null) return;

        var point = control.PointToScreen(new Point(control.ActualWidth, control.ActualHeight));
        window.Top = point.Y;
        window.Left = point.X;
    }
}

Just add this into one of your helper library and include it to the code you wish to use.

Here is a usage example.

private void Button_Click(object sender, RoutedEventArgs e)
{
    var button = e.OriginalSource as Button;
    if (button == null) return;

    var window2 = new Window2();
    window2.PlaceWindow(button);
    window2.ShowDialog();
}

What happens here is that on the Button Click event, I will create a new window "Window2" and use the extension method above to place the window relative to the button (in this case, the button that has triggered the click event. Then the new Window gets shown. You will notice that the new window will be placed on the bottom right most corner from the button. You can make some adjustments so that the new window could be place at the center-bottom of the button by modifying code like so:

public static class WindowHelper
{
    public static void PlaceWindow(this Window window, FrameworkElement control)
    {
        var point = control.PointToScreen(new Point(control.ActualWidth/2.0, control.ActualHeight));
        window.Top = point.Y;
        window.Left = point.X - (window.Width/2.0);
    }
}

I hope this is the solution you're looking for.

EDIT

Sorry, my answer might be a bit over elaborate for your needs. Here is a simpler way of achieving what you need under your context.

private void Topics_Click(object sender, RoutedEventArgs e)
{
    TreeView tree = new TreeView();

    FrameworkElement control = e.OriginalSorce as FrameworkElement;

    if(control != null)
    {
        var point = control.PointToScreen(new Point(control.ActualWidth, control.ActualHeight));
        tree.Top = point.Y;
        tree.Left = point.X;
    }

    tree.Show();
}
Tri Q
thanks for your help. i think, i couldnt tell well what i want. i want a new window will be placed under button. so i couldnt make it work. have you got another suggestions? Please let me know.
neki284
A: 

Hi TriQ;

thank for yor interest.

As i explained before; i want a new window popsup behind button not in front of; when i click the button.

when i write codes; window.show() it shows in front of the button.

http://www.freeimagehosting.net/image.php?de9869e063.jpg i want it looks like that.

Thanks in advance.

neki284
A: 

Hey,

Basically wpf orders FrameworkElements on the time that they were added, because you added the button before you added the treeview, it's visible on top. You need to change the ordering.

You should add your treeview before you add the button to your layout.

Then you do tree.Visibility = Windows.Visibility.Hidden on the TreeView(either in xaml or in your code).

When you click the button you update the values of the treeview and then do tree.Visibility = Windows.Visibility.Visible

I don't think you really want a window, you just need a panel to display some other controls that appears behind your button. Beneath this is the code for that.

Update example here:

Xaml:

<Grid>
    <Grid Visibility="Hidden" Background="black" Name="popupGrid" Width="300" HorizontalAlignment="Left" Margin="60.018,124.323,0,38.583">
        <TextBlock Padding="10,10,5,5" TextWrapping="Wrap" Foreground="White" TextTrimming="None">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut urna sit amet eros accumsan tristique. Integer sed odio velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec eget odio fringilla odio varius ultricies. Nullam scelerisque tellus vitae mauris accumsan consectetur rutrum et turpis. Proin eget blandit tellus. Mauris adipiscing consequat cursus. Morbi eu tortor metus. Fusce nec rutrum eros. Duis congue elementum risus, sit amet fermentum elit rhoncus dignissim. Suspendisse aliquet felis nec sem venenatis mattis.
        </TextBlock>
    </Grid>
    <Button Click="Button_Click" Content="Show!" Height="36.586" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="71.45" Margin="37.154,0,0,12"></Button>
</Grid>

And then in the Button_Click handler:

private void Button_Click(System.Object sender, System.Windows.RoutedEventArgs e) 
{ 
    if (sender != null) { 
        if (popupGrid.Visibility == Windows.Visibility.Visible) { 
            popupGrid.Visibility = Windows.Visibility.Hidden; 
        } 
        else { 
            popupGrid.Visibility = Windows.Visibility.Visible; 
        } 
    } 
}
Jark
the problem is; tree is not a control, it is wfp window.
neki284
is there any possibility to add a window like a control inside another window ?
neki284
Hmm the TreeView Control you are using is an control --> http://msdn.microsoft.com/en-us/library/system.windows.controls.treeview.aspx so you can add that one to a grid in your window, i checked adding it to xaml. I'm just saying that you should define the TreeView you want displayed(or other usercontrol) before you define your button. I'll update my answer in an hour or so with a testcase :)
Jark
A: 

Have a look at this blog on controlling the z-order of elements.

By setting the zindex of your popup to be less than that of the button it should appear behind it.

ChrisF
hi Crhris, i examined this blog and i am so pleased when i saw codes. i tried codes but i didt make it work.window2 ka=new window2(); ka.Left = 100; ka.Top = 300; Canvas.SetZIndex(ka,1); Canvas.SetZIndex(button, 0); ka.Show(); can you show where i made it wrong thank you
neki284
@neki284 - I've checked and it's a blog from 2006, so it looks like the code might well be out of date. I found the blog via Google - search for "wpf zindex" or "wpf zorder".
ChrisF