views:

246

answers:

2

Hi Guys,

I've been trying to create a basic behavior in my PRISM based silverlight project. Something like http://csharperimage.jeremylikness.com/2009/10/silverlight-behaviors-and-triggers%5F09.html

the problem I am having is that the OnAttached() and OnDetaching() methods of the behavior get called fine but when I set up my event handler for example AssociatedObject.KeyDown += _TextBoxFilterBehaviorKeyDown;

The associated method will never get called. I figured it may have something to do with it not being a standard silverlight project and based on PRSIM.

Has anyone else had this?

Thanks for your time

A: 

PRISM has nothing to here I think. I would suggest that you put debug points in the OnAttached, OnDetached and TextBoxFilterBehaviorKeyDown methods, in order to check that effectively all the code is being invoked at the appropriate time.

By the way the link you have provided is not working, but maybe the information in this one helps you: http://www.nikhilk.net/Silverlight-Behaviors.aspx

Konamiman
A: 

Thanks for the quick reply Konamiman, this is what I have so far:

public class TextEntryBehavior : Behavior<TextBox>
{
    public TextEntryBehavior() : base()
    {
    }

    protected override void OnAttached()
    {
        AssociatedObject.MouseLeftButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp);
        base.OnAttached();
    }

    void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        throw new NotImplementedException();
    }


    protected override void OnDetaching()
    {
        base.OnDetaching();
    }}

The OnAttached method is called when I breakpoint it but the AssociatedObject_MouseLeftButtonUp method is never called, I put a breakpoint in before the exception gets thrown.

The XAML looks like this:

           <TextBox Text="Hmmmm" Margin="20" >
                        <i:Interaction.Behaviors>
                            <behaviors:TextEntryBehavior></behaviors:TextEntryBehavior>
                        </i:Interaction.Behaviors>
                    </TextBox>
Suiva
Ok, it must be something to do with the fact I was trying to use it in a child window, it works other than that
Suiva