wpfxamleventsattachedbehaviors

Attached Behavior handling an Attached Event in WPF


I googled regarding this question but couldn't gather any information and I was wondering if it is possible for an attached behavior to handle an attached event??

I've an event declared in a class and a behavior that I am attaching to a TextBox control, the event will be raised when a button is clicked. I added the handler for this event in my behavior and wrote the logic in the event handler, but it is not executed. So, I was wondering if it is possible for an attached behavior to handle an attached event or not?

class ResetInputEventClass
{
    public static readonly RoutedEvent ResetInputEvent =  EventManager.RegisterRoutedEvent("ResetInput",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(ResetInputEventClass));

    public static void AddResetInputEventHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie == null)
        {
            return;
        }
        uie.AddHandler(ResetInputEventClass.ResetInputEvent, handler);
    }

    public static void RemoveResetInputEventHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie == null)
        {
            return;
        }
        uie.RemoveHandler(ResetInputEventClass.ResetInputEvent, handler);
    }
}

That is my Event class and this is how I am handling it in the behavior

public class MyBehavior : Behavior<TextBoxBase>
{
    public MyBehavior()
    {
        // Insert code required on object creation below this point.

    }        

    protected override void OnAttached()
    {
        base.OnAttached();
        // Insert code that you would want run when the Behavior is attached to an object.
        ResetInputEventClass.AddResetInputEventHandler(AssociatedObject, OnResetInputEvent);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        // Insert code that you would want run when the Behavior is removed from an object.
        ResetInputEventClass.RemoveResetInputEventHandler(AssociatedObject, OnResetInputEvent);
    }

    private void OnResetInputEvent(Object o, RoutedEventArgs e)
    {
        //Logic
    }
}

Here is my XAML Code:

<Grid x:Name="LayoutRoot">
    <StackPanel>
        <TextBox Margin="5" Text="Bye" TextWrapping="Wrap" Width="150">
            <i:Interaction.Behaviors>
                <local:MyBehavior/>
            </i:Interaction.Behaviors>
        </TextBox>
        <TextBox Margin="5" Text="Bye" TextWrapping="Wrap" Width="150">
            <i:Interaction.Behaviors>
                <local:MyBehavior/>
            </i:Interaction.Behaviors>
        </TextBox>
        <Button Name="MyButton" Content="Save" Width="50" Height="25" Click="MyButton_Click"/>
    </StackPanel>
</Grid>

and I am raising the event in the click event of my button

private void MyButton_Click(object sender, RoutedEventArgs e)
{
    RoutedEventArgs eventArgs = new RoutedEventArgs(ResetInputEventClass.ResetInputEvent,e.OriginalSource);
    RaiseEvent(eventArgs);
}

Solution

  • Of course it is possible. Show me your XAML and I ll tel you how an attached event triggers an attached behavior.

    Edited:

    I dont see the need why you using attached behavior and attached events because you could do everything in code behind.

    Here is how to do everything in code behind:

    Here is XAML without attached properties:

    <Grid>
            <StackPanel>
                <TextBox x:Name="txtBox" Margin="5" Text="Bye" TextWrapping="Wrap" Width="150"/>
                <Button Name="MyButton" Content="Save" Width="50" Height="25" Click="MyButton_Click"/>
            </StackPanel>
    </Grid>
    

    This is code behind.

        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            this.txtBox.Text = "hello";
        }
    

    Because you have set Name property on TextBox and Button you can access them from code behind in your Window.cs and you can write your handler easly.

    Here is how you can do everything with attached properties:

    This is the new XAML for the solution with attached properties. I had to create my custom Interaction because the one you are using is Expression Blend or silverlight and not pure WPF.

    <Grid x:Name="LayoutRoot">
            <StackPanel i:Interaction.Behaviour="True">
                <TextBox x:Name="txtBox" Margin="5" Text="Bye" TextWrapping="Wrap" Width="150"/>
                <Button Name="MyButton" Content="Save" Width="50" Height="25" Click="MyButton_Click"/>
            </StackPanel>
        </Grid>
    

    I had to set Behavior on True because the default value is false and when value is not equal to the old then the propery changed event will be called with my custom logic like this:

    private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs eventArgs = new RoutedEventArgs(ResetInputEventClass.ResetInputEvent,e.OriginalSource);
            RaiseEvent(eventArgs);
        }
    
     public class Interaction : DependencyObject
    {
        // Using a DependencyProperty as the backing store for Behaviour.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BehaviourProperty =
            DependencyProperty.RegisterAttached("Behaviour", typeof(bool), typeof(Interaction), new PropertyMetadata(false, new PropertyChangedCallback(OnBehaviourChanged)));
    
        private static void OnBehaviourChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            StackPanel sp = (StackPanel)d;
            sp.Dispatcher.BeginInvoke(new Action(() =>
            {
                TextBox tb =  VisualTreeHelper.GetChild(sp, 0) as TextBox;
    
    
                ResetInputEventClass.AddResetInputHandler(sp, new RoutedEventHandler((o, a) =>
                {
                    // Do here whatever you want, call your custom expressions.
                    tb.Text = "hello";
                }));
    
            }), System.Windows.Threading.DispatcherPriority.Background);
        }
    }
    

    Inside property changed event which will be called as I already mentioned when I change false to true. I wait till everything is intialized by telling the dispatcher to execute my code when application is in background. Then I find the TextBox and inject the handler which will be called when you trigger ResetInput event.

    This is very complicated solution but it will work with attached events and attached properties.

    I highly recommend you to use the code behind for this scenario.

    Also you made a mistake inside your ResetInputEventClass class. Add and Remove methods are not correctly spelled.

    This is how you should have written them:

    public static void AddResetInputHandler(DependencyObject d, RoutedEventHandler handler)
        {
            UIElement uie = d as UIElement;
            if (uie == null)
            {
                return;
            }
            uie.AddHandler(ResetInputEventClass.ResetInputEvent, handler);
        }
    
        public static void RemoveResetInputHandler(DependencyObject d, RoutedEventHandler handler)
        {
            UIElement uie = d as UIElement;
            if (uie == null)
            {
                return;
            }
            uie.RemoveHandler(ResetInputEventClass.ResetInputEvent, handler);
        }
    

    Have fun, I hope I helped you out.

    You could also have achieved this with Commands