Skip to main content
Home Forums General Silverlight Installation and Setup How "XamlReader.Load" handles event attributes declared in XAML?
6 replies. Latest Post by addanki on July 22, 2009.
(0)
addanki
Member
14 points
45 Posts
06-15-2009 5:23 AM |
I am working on Silverlight 3beta.
Is "XamlReader.Load" handles event attributes declared in XAML?
I came across this article at (Option3a on the page) http://blogs.windowsclient.net/rob_relyea/archive/2009/06/03/xaml-events-in-compiled-and-uncompiled-scenarios.aspx
But, it's possibility with the working is not provided. Do any one let me know on this??
I want to display XAML content on the user control dynamically, I achieved this with the help of XamlReader. But, I want to handle the events inside the XAML in the usercontrols code file.
XAML content:
<Button x:Name="OK" Click="Button_Click" ..... />
I want to handle Button_Click event in the usercontrol's code file.
please let me know on this?
PauloTio...
156 points
32 Posts
06-15-2009 1:33 PM |
You can not set the event handler in markup if you are loading your XAML via the XamlReader.
Mog Lian...
Star
14801 points
1,417 Posts
06-18-2009 11:58 PM |
one way to handle event is attach eventhandler in code.
var ele1 = XamlReader.Load(xamlstring) as FrameworkElement; var btn1 = ele1.FindName("OK") as Button; btn1.Click+=new RoutedEventHandler(btn1_Click);
06-19-2009 1:39 AM |
Hi,
Consider, there are n number of buttons whose IDs are not known, then how to solve this?
In this XAML file is provided at the runtime which is ambiguous, it may have button, or buttons, or usercontrol with buttons inside
Any how thank you for your reply, i solved this in two ways... :-)
one is with the help of 'VisualTreeHelper’'
an anotherone is with the help of supporting XML file..
Thanks &Regards
Ashok
06-19-2009 1:55 AM |
Not so long ago I was faced with a similar situation.
You can definitely use the VisualTreeHelper but I didn't use that approach.
What I did was I added an ObjectCollection in the Resources section of my XAML. This ObjectCollection (x:Key=myControlList) contain String objects which represent the x:Name of the controls in my XAML which I want to attach events to.
Once the XAML has loaded, I get the values of myControlList and find the controls and then attach their event handlers.
spiderma...
220 points
276 Posts
07-21-2009 12:15 AM |
Hi,Ashok
I met the same problem just like you described, Could you supply some sample code about how to get the event using VisualTreeHelper please?
Thanks
07-22-2009 5:58 AM |
Hello Spiderman,
Find the attached code. In this, stream is xaml file as a stream
System.IO.StreamReader reader = new System.IO.StreamReader(stream); // System.IO.Stream stream string str = reader.ReadToEnd(); MessageBox.Show(str); UserControl ctrl = (UserControl)System.Windows.Markup.XamlReader.Load(str); #region Visual Tree Helper FrameworkElement parentt = (FrameworkElement)VisualTreeHelper.GetChild(ctrl, 0); int count = VisualTreeHelper.GetChildrenCount(parentt); for (int i = 0; i < count; i++) { FrameworkElement c = (FrameworkElement)VisualTreeHelper.GetChild(parentt, i); if (c is Button) { Button b = c as Button; b.Click += new RoutedEventHandler(Button_Click); } else if (c is TextBox) { TextBox txt = c as TextBox; txt.TextChanged += new TextChangedEventHandler(txt_TextChanged); } } #endregion //btn.Click += new RoutedEventHandler(Button_Click); if (ctrl == null) MessageBox.Show("ctrl is null"); if ((LayoutRoot != null) && (ctrl != null)) LayoutRoot.Children.Add(ctrl);
Regards