简体   繁体   English

在Windows 8应用程序中从c#代码更改资源颜色

[英]Change Resource Color from c# code in Windows 8 application

I want to change the color of a SolidColorBrush in a xaml, from c# code, while the app is running. 我想在应用程序运行时通过c#代码从xaml中更改SolidColorBrush的颜色。 (This is a Pie chart from WinRT XAML Toolkit, and I want change the color of the slices.) (这是WinRT XAML工具包中的饼图,我想更改切片的颜色。)

<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: 由于Win8应用程序中没有DynamicResource,因此这是我尝试设置失败的方法:

Color yellow = Color.FromArgb(0xff, 0xff, 0xb9, 0x01);
Application.Current.Resources["MyBrush"] = new SolidColorBrush(yellow);

How could I set the color of the resource? 如何设置资源的颜色?

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. 我认为在WPF中, DynamicResource基本上会在每次请求时创建一个新实例,并会重新评估资源依赖关系层次结构中的更改。 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. 您可以更新样式/模板以更改基本颜色,可以更改视觉状态以使它们响应将更改颜色的状态更改,可以编写附加行为(直接使用附加属性或某些开源WinRT行为)实现),您可以在数据点上进行设置,以根据事件或绑定属性更新画笔,可以遍历可视树以更新颜色,也可以创建两个几乎完全相同的具有不同颜色的叠加图表控件,并更改通过更改图表控件的可见性来显示颜色。 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!) 再一次,也许您可​​以以某种方式将饼图数据点的笔刷绑定到基础数据-我还没有检查是否可行,但是如果不能-您可以修改图表控件的代码以使您功能(这是开源的力量!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM