I work with a form where some ComboBox can be created and deleted programmatically.
When they are created, some triggers that target them are created and applied to the 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 Style button, since I no longer want the ComboBox to affect 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 is not like completing a task - if the trigger is deleted when it is active, the button remains disabled.
I suggested that the button will re-evaluate its style whenever it is given a new one. what seems to happen when a trigger is added, but not when it is removed - what am I missing here?
EDIT: Changed the code for adding / removing triggers as recommended in the HB comment . However, this problem remains.
EDIT 2: * Perhaps this is not so good so far - I continued trying to add an extra ComboBox (and trigger) and found that adding a second trigger seems to break the first one. Using this code, only the last added trigger works. Should I perhaps think that FrameworkElement triggers are a write-once collection and find another way to achieve this behavior?
source share