Link to home
Start Free TrialLog in
Avatar of wint100
wint100Flag for United Kingdom of Great Britain and Northern Ireland

asked on

ObservableCollection bound to User Control, need to get bound data in code behind

I'm developing a SL4 application that alternates between different usercontrols on timer_tick event.

Previously I was doing it the old way of passing parameters to the usercontrol. I've now stored the data I need in an ObservableCollection, which I've bound to the USerControl's datacontext. The main reason for this change being that I need the field on the UserControl fields to update automatically as the data in the collection changes due to WCF calls etc.. (Please correct me if I'm doing things wrong here..)

There are two things I need to do if possible:

1) The collection holds decimal types, and I'd like to keep it this way, but I need to add a suffix to the TextBlock that is bound to the decimal. So, the value of the decimal is 202, I need to add "kWh" to the TextBlock. If not, I'll have to look at the collection holding strings.

2 I need to GET some of the data from the DataContext in code behind of the UserControl, to do some calcs before I then use on the UI. Is this possible? Can I Get data from the datacontext in any way?

Thanks
Avatar of Gautham Janardhan
Gautham Janardhan

1) you can use a converter to modify the data in any way you want.. A converter will inherit from IValueConverter and will have two methods, Convert and convert back which can be utlizied to convert the decimal data to show as 202 kwh and then when user edits it convert back t decimal

2) Yes u can get the observable collection back from the data context -> (ObservableCollection)this.DataContext
Avatar of wint100

ASKER

I tried the (ObservableCollection<MyClass>)this.DataContext method but it always returned null.
how are you setting the data context ? can you post the code
Avatar of wint100

ASKER

i'm simply using:

myclass status = new myclass();
collection=new ObservableCollection<myclass>();
collection.add(status);


usercontrol.datacontext=collection;
Not sure then y your data context is coming as null.. You must be able to retrieve the data observable collection via usercontrol.DataContext.. Where are you trying to put the code (ObservableCollection)this.DataContext ? is it inside the user control ?
Avatar of wint100

ASKER

Yes in code behind in the usercontrol.
can you post the code where you are accessing it ? looks like you are accessing it before the data context is set.
ASKER CERTIFIED SOLUTION
Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wint100

ASKER

Thanks vbigham.

I am writing the datacontxt as shown above, then in the usercontrol code behind, I'm using the following, just after the InitializeComponent();

ObservableCollection<myclass> status=(myclass)this.datacontext;

If I bind the the ObservableColelction, the textblocks on the page are empty, although i I bind the the class, that is added to the collection, the textblocks are populated correctly. So:

test = new myclass();
usercontrol.datacontext=test;

Then later in the code, when status is populated with data, the textblocks on the usercontrol are populated, but if I bind to colelction, the textblocks stay blank, even though I add 'test' to the collection.

Could this be to do with the textblock not knowing which index of the collection to display? Because, if I was extracting data from the colelction in code I'd use:

textblock.Text = collection[0].Value.tostring();

the binding I'm using is as follows:

Text="{Binding Value}"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wint100

ASKER

I think I understand now, I'm binding to the class and it does what I need.

I really need to look into MVVM some more, and figure out the whole binding process.

Thanks everyone.
Avatar of wint100

ASKER

Accepted