views:

21

answers:

1

I have seen some great examples of using behaviours to trigger animations in Silverlight. It all seems really easy to do with Expression Blend by simply dragging behaviours onto controls on the screen. But what if my control isn't actually on the screen, since I am using a Listbox bound to a ViewModel in an MVVM pattern. The listbox items are created at runtime when things are added to a collection in my ViewModel. So how would I attach behaviours to those dynamically loaded listbox items?

+1  A: 

That it the great thing of MVVM. You can fill your properties of the ViewModel with DesignTime Data:

Example below of a property on the viewmodel that provides a list of strings and in design time it provides a list of 3 items:

    List<string> _myItems;
    public List<string> MyItems
    {
        get
        {
            if (DesignerProperties.IsInDesignTool)
                return new List<string>() { "item1", "item2", "item3" }; 
            return _myItems;
        }
        set
        {
            _myItems = value;
            NotifyPropertyChanged("MyItems");
        }
    }
Wouter Janssens - Xelos bvba
Thanks for your reply. Does that mean I could attach behaviours to those design-time items and those same behaviours would get attached to the run-time items too?
EasyTimer
Yes because you add a behavior to the ListBox or ListBoxItem but never to a specific instance of a ListBoxItem
Wouter Janssens - Xelos bvba
Fantastic! Thanks again.
EasyTimer
glad to help you
Wouter Janssens - Xelos bvba