Lets say I have an ItemsControl in a Canvas.
Each Item will have its own position, but I want all items to share that same width. So if the user changes it in one place, all items get updated. What is the cleanest way to do this?
Right now I see 2 solutions.
- Whenever the settings change, I update the width property for each item. The disadvantage is, that this takes more resources than necessary, but now the view for each item simply binds to this property.
- I add a Width property to the ViewModel that is bound to the Control containing the items. I give the ItemsControl a name (e.g. "MyItemsContainer").
Now each item can get to it via:
{Binding ElementName=MyItemsContainer, Path=DataContext.Width}
The disadvantages with the second solution are the added dependencies. First of all there has to be an ItemsControl by that name (which makes it impossible to use the item in a differently named ItemsControl) and the ItemsControl's DataContext needs to have a Width property.
I would be interested in finding alternative solutions or what experiences people have made with either solution.