locked
Public void draw another control in WPF with C # RRS feed

  • Question

  • Good afternoon
    I'm having a problem here, I created the following situation in a WPF application:
    There Control MainWindow that contains a StackPanel that serves to navigate the mainsite, I created a public command in C # that sets the control that must be opened in theStackPanel, it works perfectly if I have performed on the same MainWindow, but if I try tocall this public void another control it does not appear.

    Line code MainWindow:

     

    	public partial class MainWindow : Window
    	{
    		public MainWindow()
    		{
    			this.InitializeComponent();
    			Main_Page Main_Page = new Main_Page();
    			Muda_Pagina(Main_Page);
    		}
    
    		public void Muda_Pagina(System.Windows.UIElement Pagina)
    		{
    			this.StackPanel1.Children.Clear();
    			this.StackPanel1.Children.Add(Pagina);
    		}
    	}
    

     

    I can not call the public void Muda_Pagina other controls: (

    I Try This:

     

    <span style="white-space:pre">	</span>Main_Page Main_Page = new Main_Page();
    	Muda_Pagina(Main_Page);
    
    But i dont have success :/

     


    I seen this in other controls:



    How can I call the public void Muda_Pagina by another control?

     

    Now i try this:

    I create a class file (Master.cs) with this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Net;
    using System.Windows.Ink;
    
    
    namespace Sistema
    {
    	public class Master : Navigation_Control
    	{
    		public static readonly DependencyProperty PageChangeProperty;
    		public System.Windows.UIElement PageChange
        {
          get { return (System.Windows.UIElement)GetValue(PageChangeProperty); }
          set { SetValue(PageChangeProperty, value); }
        }
    		
    		static Master()
    		{
    			// Insert code required on object creation below this point.
    			PageChangeProperty = DependencyProperty.Register("PageChange", typeof(System.Windows.UIElement), typeof(Master),
    			new PropertyMetadata(new PropertyChangedCallback(Master.PageChanged)));
    		}
    		
    		private static void PageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
          Master myControl = (Master)sender;
    			myControl.Muda_Pagina(myControl.PageChange);
    			
        }
    	}
    }
    

    And i change the mainwindow to a control Navigation_Control and post the control in mainwindow, but the propertie dont Appears

    i dont have sucess again :(

     

    Wednesday, December 22, 2010 5:40 PM

Answers

All replies

  • Howdy,

    The event is the correct mechanism to make this work.

    You are missing just one thing from what I see here and that is an event handler in your MainPage. If your MainPage has a reference to Master then within the MainPage (or MainWindow if that is what you are using.. its a little confusing) then you need to add something similar to this:

    void MainPage()
    {
     Master MyMaster = new Master();
     MyMaster.PageChange += new RoutedEventHandler(OnPageChange);
    }
    void OnPageChange(object sender, RoutedEventArgs e)
    {
     //logic to handle the event 
     //call the Public void here!
    }
    
    

    You can look here for further information on using events.

    Hope this helps.

    Cheers


    Expression MVP

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Friday, December 24, 2010 2:29 PM
    Moderator
  • I tried but I could not here, I'll try to explain what I'm trying to do:
    First sorry for my English.

    I'm trying to learn how to create WPF applications, to create an application would have:
    A main window with a menu.
    Several child windows.
    When the user clicks on a menu button and the child window is changed, so far not aproblem because the command is still running from the main window.
    The problem is if a child window is open and the user clicks a button inside it, how do Iclose this window and open another child?

    I tried:
    Create a public command in the main window, but I can not call it from another control.
    Create a navigation control, also failed.
    Monday, December 27, 2010 6:46 PM
  • Howdy,

    You need to raise an event from your child window to the main window and then in the main window you can do what you need to. As I mentioned before, it looked to me that you were just missing the subscription to the event.

    Try this link:

    http://msdn.microsoft.com/en-us/library/ms742806.aspx

    And this one:

    http://msdn.microsoft.com/en-us/magazine/cc785480.aspx

    These  both should help you construct your events correctly in WPF.

    You are close.

     

    Cheers


    Expression MVP

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Wednesday, December 29, 2010 7:56 PM
    Moderator
  • Very Thank you!
    Thursday, December 30, 2010 5:01 PM