For adding some images to a WrapPanel, I’m trying to iterate through a collection. My TileCollection contains instances of a Tile class which has a ImageSource property.
I’ve got it by doing all the stuff step by step – Create a new image 1, assign all the properties, create a bew binding 1, assign binding 1 to image 1, add image 1 to the panel, proceed with image 2, … tons of code I want to shorten now. Here is what I’ve got so far:
For Each tile As Tile In TileCollection
MainPanel.Children.Add(New Image With {.Width = 80, .Height = 80, .Margin = New Thickness(10), .SetBinding = New Binding With {.Source = tile, Path = New PropertyPath("ImageSource"), .Mode = BindingMode.Default, .UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged)}})
Next
1) .”SetBinding” doesn’t work in object initializer expression. Is there any workaround?
2) Is there any way to add a handler in object initializer expression, for e.g. capturing mousedown events?
thx
You can create extension methods on the objects that you need that do what you want and then return the self instance again to allow chaining, effectively creating a fluent interface. You then call these methods after the object initializer expression.