WPF编程中的一个挑战 [英] One Challenge in WPF Programing

查看:44
本文介绍了WPF编程中的一个挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们:



我在WPF计划中有三个主要元素如下:



1.带有Button的UserControl。

2.带有TreeView的UserControl。

3.和我的MainWindow有一个名为GridRoot的网格。 />


当我按下第一个UserControl中的按钮时,会创建一个带有TreeView的UserControl实例。所以这里的问题是araised。我想在MainWindow的GridRoot中添加此对象(

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)

)。但是,我做不到。因为我不能从第二个UserControl访问GridRoot(因为它的私有和非静态元素)。我该怎么做?



此致

). 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

推荐答案

Mmohmmad写道:
Mmohmmad wrote:

...你能告诉我在这种编程语言中成为高手的道路吗?谢谢

…would you show me the path to be a master in this programming language. Thanks

抱歉,我不认为我能以任何令人满意的方式回答你的问题。



无论如何,谢谢你提出这个问题。我会尝试说几件事。



我的教育非常基础,与编程无关。这种教育很难找到,而且在许多其他方面,它基于这样一种观点,即知识和理解不是由任何权威机构给出的,而是应该通过兴趣,独立和批判性思维以及努力工作积极地获得。 。学生应该是自我驱动的,而不是那个会问告诉我该做什么的人。另一个重要的组成部分是非常严厉的批评,尤其是来自同行的批评,以及对这种批评的建设性态度。这不适合妈妈那些会抱怨他们受伤的人,不幸的是,这个论坛上有很多询问者。无论是代表你的观点,还是接受你的错误,修复它们,并且能够感谢那些击败你的人。



另一个重要的事情就是那么深理解是主要的,而不是许多事实/技术/模式的正式知识。另一个:用自己的双手做所有重要事情,不要相信任何现有的解决方案或意见,直到你自己完成,至少在关键的基本方面。



另一个重要的组成部分是关注最基本的方面,以便能够在需要时做具体细节。在编程中,这是一种常见的谬误,只是因为人们认为它酷而做某事。通常的琐碎谬误是做某种UI,图形等,而没有正确理解类型,实例,变量,方法和参数,异常,线程和堆栈的工作方式。这些基础知识应该被理解为非常深层次。



一个并不适合所有人的小秘密:在阅读其他人的结果之前,请尝试自己完成。即使你失败了,这也不是浪费时间。你不仅没有失去发明以前没有发明过的东西的机会,而且,当你最终阅读可用的文献时,你将能够真正理解你。至于我,我应该承认:在我对这个问题有所了解之前,我没有很好的理解文学的能力。



一个我最喜欢的建议我读过某个地方:不要成为解决问题的人!惊讶吗?我不是。如果你遇到一些问题,你应该问的第一个问题是:我们真的需要解决它吗?。认真。如果人们从来没有这样的疑虑,任何进展都是不可能的。



最后,我想向一位着名设计师展示一条建议。

我最近从一本书中找到了一篇精彩的文章,用来学习一些工业设计。它由一位非常着名的设计师撰写,成功的领先设计工作室的所有者在几个国家代表,经常获得非常昂贵的订单。虽然它主要吸引年轻设计师,但它非常适用于任何创意活动领域的学生,非常适合编程:



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:

大多数问题都在奇妙地解决了简单的方法:你需要把它拿走并制作它。例如,年轻的设计师经常写信给作者,要求他给他们一个测试任务,这样他们就可以展示自己。作者总是给他们所有相同的任务:创建自己的任务并完成它。如果一个设计师真的有价值(这意味着,可以解决问题),这个人只会带来优秀作品的样本。并且,在哪里获得这样的样品?你需要制作它们。而且,如何制作它们?从简单的事情开始,例如,在自己的冰箱中整理食物。如果冰箱里没有任何食物怎么办?拿一支铅笔画画。如果你没有铅笔怎么办?从这一点 - 你自己。



Kovodstvo ,§149

[翻译成英语是我的 - SA]

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]

那怎么样? :-)



-SA

How about that? :-)

—SA


当你创建一个用户控件时,你当然应该提供一些其功能的界面,怎么回事?



你可以随时暴露它的一些孩子,但这会严重违反封装,所以不要这样做。相反,添加 internal (在同一个程序集中使用)或 public (在声明程序集之外使用) )用户控件的属性和事件。在实现中,使用私有子控件,但不要直接暴露它们。就这么简单。







例如:



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.



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


如果第一个用户控件是Grid的子项,您应该能够通过引用第一个用户控件的Parent属性来访问Grid。即在第一个用户控件类中...



var theGrid = this.Parent as Grid;



或者,您可能必须引用Parent.Parent等。
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.


这篇关于WPF编程中的一个挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆