Reputation: 149
I want to change the color of a SolidColorBrush
in a xaml, from c# code, while the app is running.
(This is a Pie chart from WinRT XAML Toolkit, and I want change the color of the slices.)
<charting:Chart.Palette>
<charting:ResourceDictionaryCollection>
<ResourceDictionary>
<SolidColorBrush x:Key="MyBrush" Color="#4586d8"/>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="{StaticResource MyBrush}"/>
</Style>
</ResourceDictionary>
</charting:ResourceDictionaryCollection>
</charting:Chart.Palette>
Since there is no DynamicResource in Win8 apps, this is how i tried to set with no success:
Color yellow = Color.FromArgb(0xff, 0xff, 0xb9, 0x01);
Application.Current.Resources["MyBrush"] = new SolidColorBrush(yellow);
How could I set the color of the resource?
Upvotes: 2
Views: 644
Reputation: 31724
I think in WPF DynamicResource
would basically create a new instance every time it's requested and would reevaluate on changes in the resource dependency hierarchy. Depending on what you want done - you would approach it differently in your case. You can update the styles/templates to change the base colors, you can change visual states to make these respond to a state change that would change the colors, you can write an attached behavior (using an attached property directly or some open source WinRT behaviors implementation) that you would set on your data points that would update the brushes based on event or bound property, you can traverse the visual tree to update the colors based or you can create two almost identical, overlaid chart controls with different colors and change the colors by changing visibility of the chart controls. Then again maybe you could somehow just bind the brush of a pie chart data point to the underlying data - I haven't checked if that's possible, but if it isn't - you could modify the code of the chart controls to give you that feature (it's the power of open source!)
Upvotes: 1