1

In WPF application it is easy to access MainWindow content but how to do in usercontrol.

In WPF Application, if my MainWindow name is Window1 and i have to access its one of the string variable named CName from some other class in same application.

 Window1 ww = Application.Current.MainWindow as Window1;

 String reqstring = ww.CName ;

I want to do same thing in usecontrol too.

Assume my usercontrol name is ABCcontrol and first/main Class name is ABCconrolLib

namespace ABCcontrol
{

  public partial class ABCcontrolLib : UserControl
  {
    Public String CName = "ABC";

    //....

  }
 }

Now i want to access this CName in some other class of same usercontrol by using Application.Control.MainWindow or by some other similar way

Please help me out as this is my first UserControl in WPF

3
  • How and when are you setting this CName property in UserControl? Commented Sep 21, 2014 at 10:51
  • You want to access the Window inside UserControl in XAML code or code behind? Commented Sep 21, 2014 at 10:53
  • want to access window/content in code/ some other class function. I can very much access in WPF Application but not able to do same in user control. Commented Sep 21, 2014 at 11:09

1 Answer 1

2

You can access Application.Current.MainWindow because you have just one instance of MainWindow in your whole application so they defined it as a static property. If you have the same characteristic for your users control (just you need to use a single instance of it in the whole application) you can define a static property there as well. Something like : then you can access name property like UserControl1.Instance.CName

 public partial class UserControl1 : UserControl
        {
            public static UserControl1 Instance
            {
                get;
                private set;
            }
     Public String CName = "ABC";
            public UserControl1()
            {

                if (Instance != null)// there should be just one instance
                    throw new NotSupportedException();
                Instance = this;

                InitializeComponent();
            }
        }

if you get error then you do not have just one instance of your usercontrol, you have to define a List and register each instance of your users controls there. Something like:

 public partial class UserControl1 : UserControl
    {

        public UserControl1()
        {
            (App.Current as App).UserControl1List.Add(this);

            InitializeComponent();
        }
    }
     public partial class App : Application
    {
        public App()
        {
            UserControl1List = new List<UserControl1>();
        }
        public List<UserControl1> UserControl1List
        {
            get;
           private set;
        }
    }

then you can reach to each instance like (App.Current as App).UserControl1List[0]... or you can use a dictionary for storing each UserControl there.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for efforts, but it is in multi threaded environment :( , it is giving error if i use same user control more than once
Look at the edited answer, here you are using a list inside your App class (or somewhere else) then you register each instance of your user control there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.