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