Basic Question
What I am trying (and failing) to do is pretty simple conceptually. I have an ArrayList of brushes as a resource, and I would like to bind an individual element’s color to an element at a specific index in the ArrayList. If possible, I would like to get the index to bind to from the element’s datacontext.
What it would look like is something like this:
<Line Stroke={DynamicResource DefaultGraphBrushes[0]}/>
I have tried a number of permutations of this (explicitly setting the ResourceKey, using quotes, ‘.’, etc), and I am hardcoding the index for now to see if I can even do this. I would ideally like to get the index from the datacontext of the line.
Context
I am trying to do this because I have a graph control that takes in an arbitrary number of data sets and plots them on a grid. Some of the data sets have colors defined in the view model that they should always be shown in, some do not. For the ones that do I am simply binding to the color they provide and that works fine. For the ones that do not I need to pick a unique color from a predefined list of colors. The application has several different “themes” available, and the list of colors to use for the graph is different for each theme in order to make it readable and pretty. The brush the line is using needs to update when the user changes themes.
In the view model for each data set it knows whether it has an assigned color or not, and if it does not it picks the next index in the list of predefined colors to use to prevent multiple data sets from using the same color (unless we have more data sets than colors, which is unlikely).
I have been searching for a way to do this for quite a while. Any suggestions on how to make this approach work, or on other approaches that may work better are greatly appreciated. Thanks!
sometimes i don’t understand why one should try too long to find a Xaml solution if this can be done much easier in code… Why not Bind your Stroke to a property that computes the Color ? code might access as well to resources (with TryfindResource or SetResourceReference). if you use tryfindresource, NotifyPropertyChanged if resource changed.