Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:44:24+00:00 2026-06-12T08:44:24+00:00

When you are adding and removing event handlers and you want to have additional

  • 0

When you are adding and removing event handlers and you want to have additional parameters, how do you go about doing this? The below code is like something i would want but obviously does not work.

How would you go about this? – it’s troublesome that i cant use delegates or lambdas as i need to also remove the handler.

    private static void IsDefaultChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        if ((bool)args.NewValue)
        {
            Window.Current.CoreWindow.KeyUp += CoreWindowOnKeyUp(dependencyObject);
        }
        else
        {
            Window.Current.CoreWindow.KeyUp -= CoreWindowOnKeyUp(dependencyObject);
        }

    }

    private static void CoreWindowOnKeyUp(CoreWindow sender, KeyEventArgs args, DependencyObject dependencyObject)
    {
        ((ICommand)dependencyObject.GetValue(Button.CommandProperty)).Execute(null);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:44:26+00:00Added an answer on June 12, 2026 at 8:44 am

    What you can do is:

    • Keep some collection of DependencyObjects that you add to/remove from in your IsDefaultChanged method.
    • Modify your CoreWindowOnKeyUp handler so it only accepts the expected two parameters, and queries the collection of DependencyObjects itself. Bind this event once and don’t unbind it.

    I.e. rather than having an event with a handler for each current dependency object, you have an event with a single handler that queries a collection for current dependency objects.

    HashTable<DependencyObject> current = new HashTable<DependencyObject>();
    
    // Bound as before
    private static void IsDefaultChanged(
        DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs args)
    {
        if ((bool)args.NewValue)
            current.Add(dependencyObject);
        else
            current.Remove(dependencyObject);
    }
    
    // Permanently bound, once.
    private static void CoreWindowOnKeyUp(CoreWindow sender, KeyEventArgs args)
    {
        foreach(var do in current)
        {
            ((ICommand)do.GetValue(Button.CommandProperty)).Execute(null);
        }
    }
    

    Alternatively, if Richard’s code doesn’t unbind lambdas properly, you can keep a Dictionary<DependencyObject, EventHandler> so you can retrieve the exact lambda that was bound and unbind it accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have a functioning code for dynamically adding and removing form inputs on
Possible Duplicate: C# Adding and Removing Anonymous Event Handler suppose I have an Action
I want to know how do I go about removing individual delegates from the
This question is related to following thread. Prism RegionAdapter - Removing then Adding View
Well, I got something like this: HTML: <div id="main" class="underline" >Hello</div> JavaScript: $('div').live('click', function(){
I have an event that triggers upon a treeview node click, this then creates
I'm adding and removing a JButton on MouseEnter and MouseExit respectively. This is working
I have a question about removing all elements after the current one. here's the
I would like to disable onclick event with CSS. Possible? Let's say I have
I have this code: private void timer_Tick(object sender, EventArgs e) { timer.Stop(); for (int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.