• Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / News / View News

How to Use a Behavior to Fire Methods from Objects in Styles

+ Add to SilverlightShow Favorites
1 comments   /   posted by Silverlight Show on Apr 07, 2010
(0 votes)
Victor Gaudioso has a new video tutorial demonstrates how to develop a Behavior that can be attached to objects in or out of Styles that allows you to specify what Method they need to fire.

Recently I had a developer ask me how he could fire an event from the CheckBox that I had put in the Style of the UserControl I gave him. 

I thought about it and, the problem is that objects in a Style are not really part of the Visual Tree until the Style is attached at runtime. 



Comments RSS RSS
  • RE: How to Use a Behavior to Fire Methods from Objects in Styles  

    posted by LawBot on Apr 09, 2010 10:33


    this is a comment to the comment from Rob on the original blog post from Victor Gaudioso.

    I guess the follwing code might answer the question on how to get access to MainPage from the code behind of a behavior.

    ' Code in the behavior:
    Protected Overrides Sub OnAttached()
      AddHandler AssociatedObject.MouseLeftButtonUp, AddressOf AssociatedObject_MouseLeftButtonDown
    End Sub

    ' OnDetached not displayed

    Private Sub AssociatedObject_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
      Dim myMain As MainPage = App.Current.RootVisual
      Dim methodinvoke As MethodInfo = GetType(MainPage).GetMethod("TestMethodMainPage")
      methodinvoke.Invoke(myMain, Nothing)
    End Sub

    ' Code in MainPage.xaml.vb:
    Public Sub TestMethodMainPage()
      MessageBox.Show("This is a MainPage method")
    End Sub

    I implemented AssociatedObject_MouseLeftButtonDown in a behavior for a ChildWindow. The behavior inherits from class behavior.

    Best regards,
    Martin (LawBot/SilverLaw)

    P.S.: If you are interested in a behavior I created you can download my ChildWindowMouseScrollResizeBehavior at the Expression Gallery. You find the download there in the Behavior section.



Add Comment


Please add 5 and 8 and type the answer here: