1 Replies - 2121 Views - Last Post: 28 February 2011 - 10:50 AM Rate Topic: -----

#1 Guest_Chris*


How do I reference an existing object instance from a user control?

Posted 26 February 2011 - 01:23 PM

Hi Guys,
I need help with what I’m sure is a very basic question.
I’m beginning my first WPF project in c# that actually abstracts away the business logic, application logic et.c

Let’s say I have the following:
- Parent.xaml - This is the main application shell.
- UcChild.xaml - A User control embedded with say a couple of combo boxes on it.
- objIncident.cs - class which holds business logic – and has say two string properties “Name” and “Location”.
As far as I can tell the best place to instanciate objIncident would be in Parent.xaml’s “Loaded” event:

public objIncident CurrentIncident = new objIncident();

On the UcChild user control I intend to bind CurrentIncident’s properties (Name and Location) to the comboboxes Text property.
I’ve read that when binding to an existing instance it must always be done in code as opposed to xaml but how do I reference an instance created in the parent?

(I intend to have other user controls referencing the same instance with databinding also)

Thanks in advance for any response.

Is This A Good Question/Topic? 0

Replies To: How do I reference an existing object instance from a user control?

#2 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: How do I reference an existing object instance from a user control?

Posted 28 February 2011 - 10:50 AM


There are two approaches I would consider taking to resolve this.

The first would be to instantiate your objIncident in the UcChild.xaml as a Resource to the control. That way you can reference it from withing within the user control (since it is the one managing the object).

The second would be to do as you are doing (instantiating the object in the Parent.xaml) and in the User Control you can create a public property so that you can pass a reference to the objIncident so that the User Control can access it. In that property you can set the data context of the XAML Container that holds all of your user control's elements to the objIncident to make life a little easier on yourself.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1