Dynamically setting a value in XAML page:
Posted
by kaleidoscope
on Geeks with Blogs
See other posts from Geeks with Blogs
or by kaleidoscope
Published on Mon, 22 Mar 2010 04:27:10 GMT
Indexed on
2010/03/22
5:41 UTC
Read the original article
Hit count: 310
This is find that I came across while developing the Silverlight screen for MSFT BPO Invoice project.
Consider an instance wherein I am calling a xaml page as a popup in my parent xaml. And suppose we wanted to dynamically set a textbox field in the parent page with the values that we select from the popup xaml.
I tried the following approaches to achieve the above scenario:
1. Creating an object of the parent page within the popup xaml and initializing its textbox field.
ParentPage p = new ParentPage();
ParentPage.txtCompCode.Text = selectedValue;
2. Using App app = (App)Application.Current and storing the selected value in app.
3. Using IsolatedStorage
All the above approaches failed to produce the desired effect since in the first case I did not want the parent page to get initialized over and over again and furthermore in all the approaches the value was not spontaneously rendered on the parent page.
After a couple of trials and errors I decided to tweak the g.cs file of the Parent xaml. *.g.cs files are autogenerated and their purpose is to wire xaml-element with the code-behind file. This file is responsible for having reference to xaml-elements with the x:Name-property in code-behind. So I changed the access modifier of supposed textbox fields to 'static' and then directly set the value in popup xaml page as so:
ParentPage.txtCompCode.Text = selectedValue;
This seemed to work perfectly.
We can access any xaml's g.cs file by going to the definition of InitializeComponent() present in the constructor of the xaml.
PS: I may have failed to explore other more efficient ways of getting this done. So if anybody does find a better alternative please feel free to get back to me.
Tinu
© Geeks with Blogs or respective owner