Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Use MessageBox to display a dialog box that appears in the caller's central, obviously, to do this, MessageBox must get the parent control, so as to calculate the center position.

I have a similar problem in a page where clicking the button displays a popup, and set the page's property IsEnable to false, therefore, code must get the parent control --- page, but I do not know can not do so.

example:
C#
namespace WpfBrowserApplication1
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessagePopup.Show(this,"hello");
        }
    }
}


C#
namespace WpfBrowserApplication1
{
    public sealed class MessagePopup
    {
        public static void Show(UIElement parent, string message)
        {
            Popup window = new Popup();
            StackPanel sp = new StackPanel { Margin = new Thickness(5) };
            sp.Children.Add(new TextBlock { Text = message });
            Button newButton = new Button { Content = "button" };
            newButton.Click += delegate { window.IsOpen = false; parent.IsEnabled = true; };
            sp.Children.Add(newButton);
            sp.Children.Add(new Slider { Minimum = 0, Maximum = 50, Value = 25, Width = 100 });
            window.Child = new Border
            {
                Background = Brushes.White,
                BorderBrush = Brushes.Black,
                BorderThickness = new Thickness(2),
                Child = sp
            };
            window.PlacementTarget = parent;
            window.Placement = PlacementMode.Center;
            window.IsOpen = true;
            parent.IsEnabled = false;
        }
    }
}


I hope that the parent is get by code , not by parameters,like this:
C#
public static void Show(string mess)
{
    UIElement parent = //get the parent control --- page
    ...
    parent.IsEnable = false;
}


Thanks.
Posted
Updated 9-May-10 6:43am
v2

on childwindow(Usercontrol) load event

C#
private void onload(object sender, RoutedEventArgs e)
      {
          Window parentWindow = Window.GetWindow((DependencyObject)sender);
          if (parentWindow != null)
          {
             parentWindow.Close();
          }
      }
 
Share this answer
 
Comments
Member 12851125 25-Oct-17 7:13am    
This worked thanks
The Parent property must be set on the Popup when used in a XAML browser application (XBAP).

Set the Parent property for the popup explicitly and you should be able to access it later in your code.
 
Share this answer
 
Comments
DRAirey1 22-Aug-10 20:17pm    
What are you talking about? You can't set the Parent property on any Framework element?
Try the following.
UIElement parent = App.Current.MainWindow;


Thanks,
Vineeth
 
Share this answer
 
Comments
yuzifu 11-May-10 10:08am    
Thanks.
I want is this.
UIElement parent = this.Parent;
 
Share this answer
 
Comments
yuzifu 9-May-10 13:47pm    
if i use this method, the parent is null Forever.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900