Monday, October 1, 2012

Modifying Silverlight ComboBox border applies to all ComboBoxes

Modifying Silverlight ComboBox border applies to all ComboBoxes

I've got a behavior I am attaching to specific ComboBoxes in my Silverlight LOB app's data entry form. The behavior needs to flash the border if the selected value is changed while the control is not focused (meaning it was a change triggered in code from some other modification). In my initial testing, I just set the ComboBox.BorderBrush to a red brush and it worked fine and exactly as expected. I then wanted to make it an animation that pulses the border. The animation works, but it gets applied to every ComboBox on the screen. I'm not sure why it would happen to any others but the one that actually has the behavior. Thanks in advance!

Code:

// This is the animation method, it is called from the  // SelectionChanged handler in the behavior passing the // AssociatedObject as the ComboBox parameter. public void AnimateComboBox(ComboBox cbo) {     LinearGradientBrush myBrush = new LinearGradientBrush();     myBrush = (cbo.BorderBrush as LinearGradientBrush);      Storyboard sb = new Storyboard();     sb.Duration = new Duration(TimeSpan.FromMilliseconds(3000));      //have to animate each gradient stop     foreach(GradientStop s in myBrush.GradientStops)     {         ColorAnimation anim = new ColorAnimation();         anim.To = Colors.Red;         anim.Duration = new Duration(TimeSpan.FromSeconds(0.5));         //auto-reverse and repeat for pulsation         anim.AutoReverse = true;         anim.RepeatBehavior = RepeatBehavior.Forever;          Storyboard.SetTarget(anim, s);         Storyboard.SetTargetProperty(anim,              new PropertyPath(GradientStop.ColorProperty));          sb.Children.Add(anim);     }      sb.Begin(); } 

XAML:

<ComboBox > <!-- leaving out ItemsSource and so-forth for brevity -->     <i:Interaction.Behaviors>         <vms:AnimateSelectionChangedBehavior/>     </i:Interaction.Behaviors> </ComboBox>   <ComboBox>     <!-- this one does not have the behavior, but its border         changes with the other one. --> </ComboBox> 

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog