Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Dear Buddies:
 
I have three main elements in a WPF program as follow:
 
1. a UserControl with a Button in it.
2. a UserControl with a TreeView in it.
3. and my MainWindow which has a grid named GridRoot.
 
when I press the button in the 1st UserControl, one instance of UserControl with TreeView is created. So here the problem is araised. I want to add this object in the GridRoot of MainWindow(
GridRoot.Children.Add(uc_TreeView)
). however, I can not do that. because I donot have access to GridRoot from the 2nd UserControl (because its private and non-static element). How should I do that?
 
Sincerely Yours
Posted 31-Jan-13 10:49am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Mmohmmad wrote:
…would you show me the path to be a master in this programming language. Thanks
Sorry, but I don't think I can answer your question in any satisfying way.
 
Thank you for this question, anyway. I'll try to say just few things.
 
My education is very fundamental and not related to programming. Such education along is hard to find, and, among many other things, it's based on the idea that knowledge and understanding are not given by any authorities, but should be actively earned, through interest, independent and critical thinking and hard work. A student should be self-driven, not like the one who would ask "tell me what to do". Another important component is really tough criticism, especially from the peer, and constructive attitude to such criticism. This is not for mom's kinds who would complain that they are hurt, as very many inquirers at this forum, unfortunately, do. Either stand for your point, or accept your mistakes, fix them, and be able to say thank to those who "defeated you".
 
Another important thing is that deep understanding is the main thing, not the formal knowledge of many facts/techniques/patterns. Another one: do every important thing with your own hands, don't trust any existing solution or opinion until you work it through by yourself, at least in key fundamental aspects.
 
Another important component is focusing on most fundamental aspects, to be able to do the specifics when it is required. In programming, this is a usual fallacy, to do something just because one thinks it's "cool". A usual trivial fallacy is doing some kind of UI, graphics, etc, without proper understanding how type, instances, variables, methods and parameters, exception, thread and stacks work. These basics should be understood to extremely deep level.
 
One little secret which is not for everyone: before reading about results of others, try to do it all by yourself. Even if you fail, this is not a wasted time. Not only you are not loosing your chance to invent something which wasn't invented before, but, when you finally come to reading available literature, you will be able to really understand you. As to me, I should admit: I have not a very good ability to understand literature before I break my own teeth on the problem in question.
 
One my favorite advice I've read somewhere: "Don't be a problem solver!". Surprised? I was not. If you face some problem, the very first question you should ask is this: "do we really need to solve it?". Seriously. If people never had doubts like that, any progress would be impossible.
 
And finally, I would like to show one advice from a famous designer.
I recently found a wonderful passage from a book I use to learn some industrial design. It is written by a very famous designer, the owner of the successful leading design studio represented in several countries, often getting very expensive orders. Even though it mostly appeals to young designers, it is very well applicable to students in any creative field of activity, and very much to programming:
 
Most problems are solved in a wonderfully simple way: you need to take it and make it. For example, young designers often write to the author, asking him to give them a test task, so they could show themselves. The author always gives them all the same task: create your own task and do it. If a designer really worth something (it means, can solve problems), this person will simply bring the samples of excellent works. And, where to get such samples? You need to make them. And, how to make them? Start with something simple, for example, organize the food in your own refrigerator. And what if there is no any food in the refrigerator? Take a pencil and draw it. And what if you have no pencil? From this point — you are on your own.
 
Kovodstvo, § 149
[Translation into English is mine — SA]
How about that? Smile | :)
 
—SA
  Permalink  
Comments
Mmohmmad at 1-Feb-13 16:03pm
   
Dear SA:
Thanks for putting your time to write this solution. It is really awesome.
All of your recomendations are true. but, I completely agree with your belief of "A usual trivial fallacy is doing some kind of UI, graphics, etc, without proper understanding how type, instances, variables, methods and parameters, exception, thread and stacks work. These basics should be understood to extremely deep level." but the problem is that VS is so wide that I want to underestand all the methods, properties, constructor and ... of one class, I almost have forgotten them after getting acquainted with a new class. How you can memorize all of these?
How many hours do you spend to work with this programming language in one day? How long of your life was passed to reach this level?
I have so many questions and I donot know which one should I start with. I adore this programming language. I put all my free time on this issue for about 1 year. but I think my learning progress is not satisfiable.
Sergey Alexandrovich Kryukov at 1-Feb-13 16:40pm
   
I never try to memorize anything. You should not focus on memorizing, only on understanding. It really works. When you are trying to get to essence of things, the mind should protect you from excessive memorizing. And I don't count my working hours, frankly... As to programming languages... I think it's very important to know several of them. People focusing on one language very often learn and miss understanding of programming — do you see the point? And, unfortunately, all languages have their cockroaches in them...
Anyway, I'll be happy to help, but... ask your questions...
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

When you create a user control, you should of course provide some interface to its functionality, how else?
 
You can always expose some of its children, but this would violate encapsulation pretty badly, so don't do it. Instead, add internal (to be used in the same assembly) or public (to be used outside declaring assembly as well) properties and events of your user control. In the implementation, use the private child controls, but don't expose them directly. As simple as that.
 
[EDIT]
 
For example:
 
public class MyUserContol {
 
    public MyUserControl() {
        //...
        someButton.Click += (sender, evenArgs) => {
            if (ButtonClickHandler != null)
               ButtonClickHandler.Invoke(this, new System.EventArgs());
            // or
            // ButtonClickHandler.Invoke(this.someButton, new System.EventArgs());
        }; // this is how a private event instance is delegated to an exposed one
        // ...
    } //MyUserControl
    
    Button someButton = new Button();
    GridView gridRoot = new GridView();
 
    internal event System.EventHandler ButtonClickHandler;
    internal object GridRootSelected {
        get { return this.gridRoot.SelectedValue; } // use private child...
    } //GridRootSelected
   
    // and so on...

} //class MyUserContol
—SA
  Permalink  
v3
Comments
Mmohmmad at 31-Jan-13 16:05pm
   
Dear SA:
 
THanks for your answer. May you please provide me an example of this as a form of project?
Thanks.
Sergey Alexandrovich Kryukov at 31-Jan-13 16:16pm
   
I cannot describe all you may need, but please see my update answer, after [EDIT].
Are you getting the picture?
—SA
Mmohmmad at 31-Jan-13 16:25pm
   
sorry that I change the path of my question. but each time I see your answers I am amazed and I reach to a belief that you are a master in this programming language. I think these are somehow professional. how did you learn these things? would you show me the path to be a master in this programming language.
Thanks
Sergey Alexandrovich Kryukov at 31-Jan-13 16:32pm
   
Thank you for your nice words.
If so, will you accept my answer formally (green button) — thanks.
—SA
Sergey Alexandrovich Kryukov at 31-Jan-13 17:07pm
   
Anyway, thanks for this question. Too bad it's not possible to answer well enough to be really useful. I tried to do what I can.
Please see Solution 3.
What do you think?
—SA
Mmohmmad at 31-Jan-13 16:39pm
   
Yes of course, It will be my plessure. but if so, will you answer my question :)?
thanks?
Sergey Alexandrovich Kryukov at 31-Jan-13 17:08pm
   
Just tried, in Solution 3. Sorry, it cannot be a recipe... :-)
Hope it's just interesting enough.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If the 1st user control is a child of the Grid, you ought to be able to get to the Grid by referencing the Parent property of the first user control. I.e. within the 1st user control class...
 
var theGrid = this.Parent as Grid;
 
Or, you may have to reference Parent.Parent, etc.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 410
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 235
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 31 Jan 2013
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