Posts Tagged DataBinding

Silverlight VisualState changes using DataBinding not code behind


Download
I hate the fact that I have to keep using code behind to trigger VisualState changes in my view.  Frequently I just want to change a visual state of a control based on the properties of the object to which the control is bound – such as making additional options visible or invisible, changing the mode of the display based on information stored in the model etc.

So I’ve come up with a control that helps!  The VisualStateController can be placed in the XAML for a control and be bound to a property of the databound object using its StateMode dependency property. 

  • If the property is a bool then two further properties OnState and OffState are used to determine the visual state of the control based on the value of the property.
  • If the property is an enum then the visual state will be set the the text string name of the enum value.
  • If the property is a string it will be used to directly set the visual state

Now changes causing normal binding updates will automatically trigger the visual state changes.

<VisualStateCtrls:VisualStateController StateMode="{Binding IsSelected}"
             OnState="Selected"
             OffState="Unselected"/>

<VisualStateCtrls:VisualStateController StateMode="{Binding IsResizable}"
             OnState="Sizers"
             OffState="NoSizers"/>

The VisualStateController binds automatically to the UserControl (or Control) that it is a child of.  You can put it inside any panel within the body of your UserControl’s XAML.

You can find the project and source code here.

If you want to achieve the same thing using triggers and a slightly more wordy approach, you can use blend and triggers – see here for details.

, , , , , , ,

5 Comments

Silverlight ComboBox loses selection on ItemsSource update


Download

I’ve been beating my head against a number of problems with the Silverlight project I am working on – but one of the worst was the amount of spaghetti you need to write if you are going to use Combo boxes or other ItemsControls whose source may change but you want to persist the current selection.  I figured that there had to be a better way, but I couldn’t find anything on Google.  

Eventually I bit the bullet and hijacked Silverlight’s binding system and replaced the bindings for ItemsControls with my own version that replaces the value after updating the source.  It’s nice because you wire the binding like normal and then just call “Hook” on my class and it does the rest for you! Woohoo, data grids with comboboxes that can update…. That’s going to save me a few of the hours I burned writing this thing.  

You start the hook by defining an instance in your UserControl and calling Hook from the _Loaded event, or anywhere after the tree has been formed and the initial bindings made.  That’s pretty much it – you can supply a delegate if you want to fix up your own list and you can hijack the ItemControls method of selecting to force a search of the list using the Equals override.

_hook.Hook(this); //!!!!!!

Source files here

, , , ,

10 Comments