0

I want to create an array resource of colors in XAML where each color is defined as a dynamic resource. I think that it can be done, but I can't figure out the syntax.

I have tried this:

<x:Array Type="Color" x:Key="Colors">
    <Color>
        <DynamicResource ResourceKey="BasicBlueColor" />
    </Color>
</x:Array>

But it doesn't work, because dynamic resource can only be assigned to dependency property.

And this just simply doesn't work, but I think it describes well what I am trying to do:

<x:Array Type="Color" x:Key="Colors">
    <Color>{DynamicResource BasicRedColor}</Color>
</x:Array>

Clarification:

  • I can't use static resources over dynamic because the colors are defined in referenced assembly which is out of my control.
  • The resource I am trying to create must be of type IList.

Edit 2: I was under impression that the resources are defined as static or dynamic and that they must be used as defined. My thanks to @Sham for explaining it to me.

0

3 Answers 3

3

You could reference the Color resources using StaticResource. This works:

<Color x:Key="BasicRedColor">Red</Color>
<Color x:Key="BasicGreenColor">Red</Color>
<x:Array Type="Color" x:Key="Colors">
    <StaticResource ResourceKey="BasicRedColor" />
    <StaticResource ResourceKey="BasicGreenColor" />
</x:Array>

If you want to to be able to switch colours at runtime, you will have to replace or modify the Color objects in the array programmatically.

An array is an array that may or may not contain some elements. It's not some kind of dependency object.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. I updated my question with clarification.
I don't understand your first remark and disagree with your second one.
1

Use ResourceDictionary to place multiple resources.

You may create one ResourceDictionary with name "ApplicationNameColors" and use these keys wherever required. Don't forget to add this to application/windows/etc resources before using.

DynamicResource is very different mechanism than the things you are talking about. It's recomended to use DynamicResource when your style is dependent on windows setting because DynamicResource may cost the application performance.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfApp1">
   <SolidColorBrush x:Key="Color1" Color="#555555" />
   <SolidColorBrush x:Key="Color2" Color="#555555" />
   <SolidColorBrush x:Key="Color3" Color="#555555" />
   <SolidColorBrush x:Key="Color4" Color="#555555" />
   <SolidColorBrush x:Key="Color5" Color="#555555" />

</ResourceDictionary>

6 Comments

Thank you for the answer. I updated my question with clarification.
@kwitee Nice, you don't need DynamicResource even if colors are defined in other assembly because assembly is known at compile time.
The color is defined in the other assembly as dynamic resource. Can I make it static on my end?
@kwitee Could you share the sample xaml for those dynamic resources?
Resources are not static or dynamic when you define them. Static and Dynamic are mechanism to use. Go through the link learn.microsoft.com/en-us/previous-versions/dotnet/… for details.
|
1

It's impossible, since dynamic resource should be used for dependency property in objects derived from DependencyObject, but x:Array is not a dependencyobject, this is what error says:

A 'DynamicResourceExtension' cannot be used within a 'ArrayList' collection. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.