Click here to Skip to main content
12,076,925 members (46,974 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF
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:
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");
        }
    }
}

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:
public static void Show(string mess)
{
    UIElement parent = //get the parent control --- page
    ...
    parent.IsEnable = false;
}

Thanks.
Posted 9-May-10 7:42am
yuzifu157
Edited 9-May-10 7:43am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

UIElement parent = this.Parent;
  Permalink  
Comments
yuzifu 9-May-10 13:47pm
   
if i use this method, the parent is null Forever.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try the following.
UIElement parent = App.Current.MainWindow;

Thanks,
Vineeth
  Permalink  
Comments
yuzifu 11-May-10 10:08am
   
Thanks.
I want is this.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

on childwindow(Usercontrol) load event
 
private void onload(object sender, RoutedEventArgs e)
      {
          Window parentWindow = Window.GetWindow((DependencyObject)sender);
          if (parentWindow != null)
          {
             parentWindow.Close();
          }
      }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
DRAirey1 22-Aug-10 20:17pm
   
What are you talking about? You can't set the Parent property on any Framework element?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160212.1 | Last Updated 13 Jan 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100