wpf button location
I am building an application. I have a Button
on a Window
. When I click the Button
, I want
a new Window
to pop up. But, the new Window
must open under the Button
. As another way, when the Window
pops up, the Button
must been shown on the Window
. How can I do that? Is开发者_开发技巧 that possible?
Thanks in advance.Lilly
My code is as follows:
private void Topics_Click(object sender, RoutedEventArgs e)
{
TreeView tree = new TreeView();
tree.Top = 250;
tree.Left = 30;
tree.Show();
}
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();
}
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; } } }
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.
精彩评论