wint100
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
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
ASKER
I tried the (ObservableCollection<MyCl ass>)this. DataContex t method but it always returned null.
how are you setting the data context ? can you post the code
ASKER
i'm simply using:
myclass status = new myclass();
collection=new ObservableCollection<mycla ss>();
collection.add(status);
usercontrol.datacontext=co llection;
myclass status = new myclass();
collection=new ObservableCollection<mycla
collection.add(status);
usercontrol.datacontext=co
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 .DataConte xt ? is it inside the user control ?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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<mycla ss> status=(myclass)this.datac ontext;
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=te st;
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.tostri ng();
the binding I'm using is as follows:
Text="{Binding Value}"
I am writing the datacontxt as shown above, then in the usercontrol code behind, I'm using the following, just after the InitializeComponent();
ObservableCollection<mycla
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=te
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.tostri
the binding I'm using is as follows:
Text="{Binding Value}"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
I really need to look into MVVM some more, and figure out the whole binding process.
Thanks everyone.
ASKER
Accepted
2) Yes u can get the observable collection back from the data context -> (ObservableCollection)this