Search code examples
wpfxamlstaticresource

WPF - Assign static resources into XAML array without code-behind


I'm working on MS .NET WPF; In XAML, I've defined some static resources. I want to assign them into an array which is also declared in XAML.

Here are the static resources:

<local:Person x:Key="PersonABC" Name="Hello" Age="29" />
<local:Person x:Key="PersonXYZ" Name="World" Age="55" />

But I couldn't assign them into the array by something like {StaticResource PersonABC}. I've to re-create the resources and assign them into the array:

<x:Array x:Key="PersonList" Type="{x:Type local:Person}">
    <local:Person Name="Hello" Age="29" />
    <local:Person Name="World" Age="55" />
</x:Array>

But i want something like:

<x:Array x:Key="PersonList" Type="{x:Type local:Person}">
    "{StaticResource PersonABC}"
    "{StaticResource PersonXYZ}"
</x:Array>

Solution

  • StaticResource can be written in tag (element) format:

    <x:Array x:Key="PersonList" Type="{x:Type local:Person}">
        <StaticResource ResourceKey="PersonABC"/>
        <StaticResource ResourceKey="PersonXYZ"/>
    </x:Array>
    

    see docs