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

Rog*_*oid 7 wpf resources xaml

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

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

Moj*_*ter 6

除非我误解了这种情况,否则你所谈论的确实很有效.我刚用这个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>
Run Code Online (Sandbox Code Playgroud)

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

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

它就像一个冠军.

  • 有没有人发现如何在资源字典中使用它? (2认同)