tags:

views:

164

answers:

1

I get exception when declare resources in this order:

<Window.Resources>
    <sys:Object x:Key="resourceA"></sys:Object>

    <x:Array x:Key="resourceB" Type="sys:String">
        <sys:String>foo</sys:String>
    </x:Array>
</Window.Resources>

and when declare this way, all works:

<Window.Resources>
    <x:Array x:Key="resourceB" Type="sys:String">
        <sys:String>foo</sys:String>
    </x:Array>

    <sys:Object x:Key="resourceA"></sys:Object>
</Window.Resources>

The Exception thrown is:

Cannot convert the value in attribute 'ItemsSource' to object of type 'System.Collections.IEnumerable'. 'System.Windows.Markup.ArrayExtension' is not a valid value for property 'ItemsSource'. Error at object 'System.Windows.Controls.ComboBox' in markup file 'WpfResourcesBug;component/window1.xaml' Line 18 Position 37.

Full code:

<Window x:Class="WpfResourcesBug.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <sys:Object x:Key="resourceA"></sys:Object>

        <x:Array x:Key="resourceB" Type="sys:String">
            <sys:String>foo</sys:String>
        </x:Array>
    </Window.Resources>

    <StackPanel>
        <ComboBox SelectedIndex="0" ItemsSource="{StaticResource resourceB}" />
    </StackPanel>
</Window>
+2  A: 

I think you might find a possible solution here

bioskope
I tried {Binding Source={StaticResource resourceB}}, but get no items in combobox. Exception is not thrown, but problem is not solved. And again, if resources declaration order changed, it works fine in both cases.
alex2k8
I think it again boils down to the Array not being resolved into an enumerable collection. Basically thats where your previous error message stems from. Have you thought about going for the ObjectDataProvider approach? Something likehttp://www.codeproject.com/KB/WPF/FillComboboxWSortedEnum.aspx
bioskope
If we just remove resourceA, the Array successfully resolved into an enumerable collection. So the problem is no in Array itself, but in resourceA, and even in declaration order. This is what makes no sence for me.
alex2k8