Access Control Properties from Another Class in C # WPF

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

I have two controls (DatePickers from the WPF toolbar by default) that are in different windows, so in different classes. I can easily access these control properties, such as datePicker1.Text , from my native class, that is, in its native window, but when I try to reach datePicker1.Text from another window, I get nothing.

I am trying to assign the value of one datePicker to another using a window link in my code:

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

and does not work

I also tried to do this through a public property of the class, for example:

 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 not assigned a value.

Please help me understand this basic problem. I know that there are other ways to access values ​​in WPF, such as data binding, but I would like to understand the basics first.

+4
source share
3 answers

I am using VS 2010 beta 2 right now, which leads to crashes regularly performing the simplest WPF encoding, for example, trying to duplicate your question code :): but note:

Is it possible that using this syntax will "go right":

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

Edit 1: Well, I got a WPF replica of your code that didn't work: using the syntax above, I can both get and set the property in a “different window”.

Edit 2: The code also works using your source code :) Which seemed to be “correct” the first time I read it. Do you set this property before reading it ?: As far as I know, the DateTimePicker Text property will be the default empty string on first creation.

Edit 3: in response to Rem request:

  • In the main window there is a button "button1: which sets the parameters and gets the value of the open DTContent property defined in the second window instance with the name:" WindowAdded: here "" Click event handler for this button in the main window window code:

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

Edit 4: the best example of the “real world”: in most cases you will want to create this instance of another window and hold it for reuse: imho: do not have it only within the scope of the Click event button. Therefore, please consider:

Somewhere within the main code window, define a "place holder" for the window (s) you add: private WindowAdded wa;

If you select the most suitable for creating an instance of this window: create an instance and assign the variable "place-holder": then reuse it as necessary. In WinForms, I most often create the required secondary windows, which I will need to reuse instance references to access any of them in the main form load or displayed events.

Discussion: of course, if you intend to create "temporary" windows, and you do not need to use this link again for a new instance of the window, and then create it as part of a function.

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

+1
source

Unfortunately, the basics of WPF are data bindings. Doing this in any other way is a “fight against the grain", this is bad practice and, as a rule, an order of magnitude more difficult to encode and understand.

In your problem, if you have data to share between views (and even if it's just one view), create a view model class that contains properties for the data view and bind the properties to your view (s).

In your code, control only the view model class and do not touch the actual presentation with visual controls and visual composition.

+2
source

As others have already pointed out, this is probably not the way to go, but you can use:

 <object x:FieldModifier="public".../> 

To set an object publicly. See msdn for details .

0
source

All Articles