如何在动态资源中更新动态资源? [英] How to update Dynamic Resource within a Dynamic Resource?

查看:54
本文介绍了如何在动态资源中更新动态资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视觉画笔,它是一组形状,其​​主要颜色是动态资源本身 - 因此形状是例如 MyShape 和颜色,MyColour 由 Shape 对象引用.
我的问题是当我为此更新颜色时 - 它仅在第一次加载形状时发生(需要先设置颜色)但是,只要我更改颜色,它就不会更新使用该颜色的动态资源- 我该如何完成这项工作?
只需要让一个动态资源在另一个动态资源中工作,并在我更改颜色时让它们都更新.
我不知道如何让它工作 - 我花了一些时间为 WPF 创建一个颜色选择器,却发现我无法改变这个项目的颜色 - 1-Tier 资源在我直接设置画笔/颜色而不是颜色的地方工作在另一个对象或 2 层资源中.

I have a visual brush which is a group of shapes, the main colour of which is a dynamic resource itself - so the shape is for example MyShape and the Colour, MyColour which is referenced by the Shape object.
My problem is when I update the colour for this - it only happens the first time the shape is loaded (the colour needs to be set first) however as much as I change the colour it won't update the dynamic resource that uses the colour - how do I make this work?
Just need to make a dynamic resource work within another dynamic resource and have them both update when I change the colour.
I have no idea how to get this to work - I spent time creating a colour-picker for WPF only to find I cannot change the colour of this item - 1-Tier resources work where I set the brush/colour directly but not a colour within another object or 2-Tier Resource.

我的问题似乎特定于在单独的资源/字典中使用这些,因为我的程序需要从类而不是窗口访问此项目,当 MyColor 位于单独的资源中时,提到的主要示例不起作用.

My problem seems to be specific to using these in a seperate Resource / Dictionary as my program needs to access this item from a class not the Window, the main example mentioned does not work when the MyColor is in a seperate Resource.

推荐答案

除非我误解了这种情况,否则您所说的确实很有效.我刚刚用这个 Xaml 试了一下:

Unless I misunderstand the situation, exactly what you're talking about works pretty well. I just tried it out with this Xaml:

<Window x:Class="ConditionalTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <SolidColorBrush x:Key="MyColor" Color="Aqua" />

        <VisualBrush x:Key="MyBrush">
            <VisualBrush.Visual>
                <Ellipse Height="50" Width="100" Fill="{DynamicResource MyColor}" />
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>
    <Grid Background="{DynamicResource MyBrush}">
        <Button Height="30" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" Content="ChangeColor" Click="Button_Click" />
    </Grid>
</Window>

然后在该按钮的点击处理程序中更改颜色:

And then changed the color in the click handler for that button:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ((SolidColorBrush)Resources["MyColor"]).Color = Colors.Purple;
}  

它就像一个冠军.

这篇关于如何在动态资源中更新动态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆