访问控制的从C#WPF另一个类的属性 [英] Access control's properties from another class in C# WPF

查看:231
本文介绍了访问控制的从C#WPF另一个类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与类之间的可见性一团糟。请帮我这个新手的问​​题。

I'm in a mess with visibility between classes. Please, help me with this newbie question.

我有两个控件(从默认的WPF工具箱DatePickers),这是在不同的窗口,所以在不同的班级。我可以很容易地从它的原生类,也就是说,在其原生窗口中访问诸如 datePicker1.Text 这些控件的属性,但是当我试图访问 DATEPICKER1。 。文字从另一个窗口,我什么也没得到。

I have two controls (DatePickers from default WPF toolbox) which are in different windows, so in different classes. I can easily access these controls properties like datePicker1.Text from within its native class, i.e. in its native window, but when I try to reach datePicker1.Text from another window I get nothing.

我试图分配一个日期选择器到另一个值,使用参考我的代码窗口:

I try to assign value of one datePicker to another, using reference to the window in my code:

string testStr;
...
AnotherWindow aw = new AnotherWindow();
testStr = aw.datePicker2.Text;
datePicker1.Text = testStr;

和它不工作

我也试图通过一类的公共属性来做到这一点,如:

also I tried to do it through public property of a class, like:

public partial class AnotherWindow : Window
{
....

public string dateNearest
    {

        get { return datePicker2.Text; }
        set { datePicker2.Text = value; }
    }
....



,然后用它在另一个窗口

and then use it in another window:

string testStr;
...      
AnotherWindow aw = new AnotherWindow();
testStr = aw.dateNearest;



也没有指派值。

but also no value assigned.

请帮助我了解这个基本问题。我知道有象绑定在WPF访问值的其他方式,但我想先了解基本知识。

Please, help me to understand this basic issue. I know there are other ways of accessing values in WPF like databinding, but I would like to understand basics first.

推荐答案

我使用VS 2010 Beta 2中,现在出故障定期做最简单的WPF编码,就像试图复制你的问题的代码:)玛:但是考虑:

I'm using VS 2010 beta 2 right now which crashes regularly doing the simplest WPF coding, like trying to duplicate your question's code :) : but consider :

是否有可能使用此语法将做正确的事情:

Is it possible that using this syntax will "do the right thing" :

    public string dateNearest 
    { 
        get { return this.datePicker2.Text; } 
        set { this.datePicker2.Text = value; } 
    }



编辑1:好吧,我得到的没你的代码的WPF复制'T崩溃:其他窗口使用上述语法我既可以领取并设置属性

Edit 1 : Okay, I got a WPF replication of your code that didn't crash : using the above syntax I can both get and set the property in the "other window."

编辑2:代码也可以使用原来的代码: )其中,在我看来是正确的,我第一次读它。你设置该属性你读它之前? :据我所知,一个DateTimePicker的Text属性将是刚创建时默认为空字符串

Edit 2 : The code also works using your original code :) Which, seemed to me to be "proper" the first time I read it. Are you setting that property before you read it ? : to my knowledge a DateTimePicker's Text property will be an empty string by default when first created.

编辑3:响应雷姆的要求:

Edit 3 : in response to Rem's request :


  1. 主窗口中有一个按钮,按钮1:哪些测试设置和获取公共财产DTContent中命名的第二个窗口的实例定义:WindowAdded:这里的Click事件处理程序在主窗口的代码按钮:

  1. the main window has a button, 'button1 : which tests setting and getting the Public Property DTContent defined in the instance of the second Window named : 'WindowAdded : here's the 'Click event handler for that button in the main window's code :

private void button1_Click(object sender, RoutedEventArgs e)
{
  WindowAdded wa = new WindowAdded();    
  wa.DTContent = DateTime.Now.ToString();
  Console.WriteLine("dt = " + wa.DTContent);
}


编辑4:更好的现实世界的例子:恕我直言:的的有只存在范围之内,你会想创建另一个窗口的实例,并抓住它,再利用大多数情况下,一个按钮的Click事件。所以考虑,请:

Edit 4 : a better "real world" example : most cases you are going to want to create that instance of another window, and hold on to it, for re-use: imho : not have it exist only within the scope of a button's Click event. So consider, please :

某处在主窗口的代码的范围定义一个占位为窗口(S),您将添加:私人WindowAdded WA;

Somewhere in the scope of the main window's code define a "place-holder" for the window(s) you will add : private WindowAdded wa;

在事件的您选择作为最适合创建窗口实例的:创建实例,并分配给你的占位符变量:然后根据需要重新使用它。在的WinForms我最常创建所需的辅助窗口,我需要重新使用引用在主窗体的负载或显示的事件来访问他们的东西的实例。

In the event you select as most appropriate for creating the instance of that window : create the instance, and assign to your "place-holder" variable : then re-use it as needed. In WinForms I most often create required secondary windows that I will need to re-use references to the instances of to access something on them in the main form's load or shown events.

讨论:当然,如果你的目的是建立临时的窗口,你不需要再重复使用,提及新窗口的实例,然后在一些功能的范围创造它是罚款

Discussion : of course, if your intent is to create "temporary" windows, and you don't need to re-use that reference to the new window's instance again, then creating it in the scope of some function is fine.

和,如果你需要在你的第二个窗口中访问的唯一的事情是的DateTimePicker,然后使用相同的技术上面建议的,但创建和坚持的一个参考例如只的DateTimePicker的。

And, if the only thing you ever need to access on your second Window is the DateTimePicker, then you use the same technique suggested above, but create and hold to a reference to the instance of the DateTimePicker only.

这篇关于访问控制的从C#WPF另一个类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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