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 7170721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:16:09+00:00 2026-05-28T15:16:09+00:00

I am working with a form where some ComboBoxes can be created and removed

  • 0

I am working with a form where some ComboBoxes can be created and removed programmatically.

When they are created, some triggers which target them are created and applied to a button:

    Dictionary<ComboBox, DataTrigger> triggers = new Dictionary<ComboBox, DataTrigger>();

    private void CreateTrigger(ComboBox box)
    {
        Style s = new Style(typeof(Button), MyButton.Style);
        foreach(TriggerBase aTrigger in MyButton.Style.Triggers)
            s.Triggers.Add(aTrigger);

        DataTrigger t = new DataTrigger 
          { 
            Binding = new Binding("SelectedItem") { Source = box }, 
            Value = null 
          };
        t.Setters.Add(new Setter(Button.IsEnabledProperty, false));
        s.Triggers.Add(t);

        triggers.Add(box, t);

        MyButton.Style = s;
    }

So far so good*. . . the problem is, what to do when the ComboBox is removed from the window. I need to remove the trigger from the button’s Style, since I no longer want the ComboBox to influence its behavior. I tried the most obvious option:

    private void RemoveTrigger(ComboBox box)
    {
        Style s = new Style(typeof(Button), MyButton.Style);
        foreach(TriggerBase aTrigger in MyButton.Style)
            if(aTrigger != triggers[box]) s.Triggers.Add(aTrigger);

        triggers.Remove(box);

        MyButton.Style = s;
    }

But this does not seem to do the job – if the trigger is removed while it is active, then the button stays disabled.

I had assumed that the button would re-evaluate its Style whenever it is given a new one. that seems to be happening when the trigger is added, but not when it’s being removed – what am I missing here?

EDIT: Changed code for adding/removing triggers as per the advice in H.B.’s comment. However, the problem in question remains.

EDIT 2: *Maybe not so far so good after all – I went on to try adding an additional ComboBox (and trigger) and discovered that adding a second trigger seems to break the first one. Using this code, only the most recently added trigger works. Should I be perhaps thinking of a FrameworkElement’s triggers as a write-once collection and finding a different way to achieve this kind of behavior?

  • 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-05-28T15:16:10+00:00Added an answer on May 28, 2026 at 3:16 pm

    It’s been a long time since this question was asked, but I figured I’d at least post how I resolved the issue for the sake of sharing:

    I never did find a way to remove triggers that worked reliably. So instead, I added a property to my View which indicated how all the triggers would have evaluated, if they had existed, and hooked a DataTrigger up to this property.

    public bool TriggerPoseur { get; set; }  // Actually notifies when it changes
    

    In place of adding and removing triggers, created an handlers to watch the properties that the triggers would have watched:

    public void ComboBoxDataContext_SelectedItemChanged(object sender, PropertyChangedEventArgs e) { //update TriggerPoseur }
    

    This sidesteps all the hassle with creating and removing triggers. Instead there’s one trigger, and adding and removing event handlers works just fine.

    (Hacky, yes.)

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

Sidebar

Related Questions

I've been working with some form processing providers and they seem to have a
im currently makeing some form you can see at http://jsfiddle.net/AnMSa/ it is working fine
I've recently begun working on a project regarding GUI building using some form of
I am working on zend. I have a form with some checkboxes. I want
I am working on a windows form project and having some problem with UserControl
I am working in a form in which there are check boxes now i
i'll try and be as clear as possible. I'm working on some form validation
I have been working on some form validation in jQuery. Everything was going nicely
I'm working on updating some form inputs to HTML5. I'm not interested in validating
I am working on a php form with some jquery, especially for the error

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.