Skip to main content

Microsoft Silverlight

Answered Question How "XamlReader.Load" handles event attributes declared in XAML?RSS Feed

(0)

addanki
addanki

Member

Member

14 points

45 Posts

How "XamlReader.Load" handles event attributes declared in XAML?

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?

PauloTioseco
PauloTio...

Member

Member

156 points

32 Posts

Re: How "XamlReader.Load" handles event attributes declared in XAML?

You can not set the event handler in markup if you are loading your XAML via the XamlReader.

//Please mark my reply as answer if it solves your question. Thanks.

Paulo T.
Software Engineer @ Apriso
LinkedIn

Mog Liang - MSFT
Mog Lian...

Star

Star

14801 points

1,417 Posts

Re: How "XamlReader.Load" handles event attributes declared in XAML?

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);

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

addanki
addanki

Member

Member

14 points

45 Posts

Answered Question

Re: How "XamlReader.Load" handles event attributes declared in XAML?

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

PauloTioseco
PauloTio...

Member

Member

156 points

32 Posts

Answered Question

Re: How "XamlReader.Load" handles event attributes declared in XAML?

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.

//Please mark my reply as answer if it solves your question. Thanks.

Paulo T.
Software Engineer @ Apriso
LinkedIn

spiderman110
spiderma...

Member

Member

220 points

276 Posts

Re: Re: How "XamlReader.Load" handles event attributes declared in XAML?

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

addanki
addanki

Member

Member

14 points

45 Posts

Re: Re: How "XamlReader.Load" handles event attributes declared in XAML?

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

Ashok

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities