简体   繁体   English

如何在动态资源中更新动态资源?

[英]How to update Dynamic Resource within a Dynamic Resource?

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. 我有一个可视刷子,它是一组形状,其​​主要颜色本身就是动态资源 - 因此形状是例如MyShape和由Shape对象引用的Color,MyColour。
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. 我不知道如何让它工作 - 我花时间为WPF创建一个颜色选择器只发现我无法改变这个项目的颜色 - 1层资源工作在哪里我直接设置画笔/颜色而不是颜色在另一个对象或2层资源中。

Edit: 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. 编辑:我的问题似乎是特定的在单独的资源/字典中使用这些,因为我的程序需要从一个类而不是Window访问此项目,当MyColor在一个单独的资源时,提到的主要示例不起作用。

Unless I misunderstand the situation, exactly what you're talking about works pretty well. 除非我误解了这种情况,否则你所谈论的确实很有效。 I just tried it out with this Xaml: 我刚用这个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;
}  

And it worked like a champ. 它就像一个冠军。

Can you post an example of how you are attempting to change the color in the resource dictionary? 您可以发布一个示例,说明您是如何尝试更改资源字典中的颜色的吗?

When I make a sample app and try to change the resource value it appears that the SolidColorBrush in the resource dictionary has been frozen so it can't be modified. 当我创建一个示例应用程序并尝试更改资源值时,似乎资源字典中的SolidColorBrush已被冻结,因此无法修改它。 To get around this I just set the new value to a new SolidColorBrush. 为了解决这个问题,我只需将新值设置为新的SolidColorBrush。

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

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