Click here to Skip to main content
Rate this: bad
good
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 at 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 at 11-May-10 10:08am
   
Thanks.
I want is this.
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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 11 May 2010
Copyright © CodeProject, 1999-2014
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