views:

228

answers:

1

I've been trying to solve this problem for over an hour now, and can't figure it out. Hopefully someone can see what I'm doing wrong.

I have two separate projects, both of which populate a combobox with an array of Doubles in the UserControl.Resources section, then databind to it in the GUI. What I'm doing is essentially just this, which works fine in kaxaml and in one of my two projects.

<Page>
  <Page.Resources>
    <x:Array x:Key="Increments" Type="sys:Double">
      <sys:Double>0.01</sys:Double>
      <sys:Double>0.02</sys:Double>
      <sys:Double>0.03</sys:Double>
      <sys:Double>0.04</sys:Double>
    </x:Array>
  </Page.Resources>

  <Grid>  
    <ComboBox ItemsSource="{StaticResource Increments}" />
  </Grid>
</Page>

The other project gives me the following error:

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 ...

I cannot figure out why this is happening. I've tried looking at the schemas referenced in both XAML files, but they are the same... I don't have any errors or messages in the Output window. I got desperate and ran it through FxCop to see if it would catch something related, and although it has caught several valid errors, none of them were related.

+1  A: 

I had to wrap this in an ObjectDataProvider to get it to work, and replace the StaticResource with a binding to the StaticResource:

<!-- Resources -->
<ObjectDataProvider x:Key="Incs2">
  <ObjectDataProvider.ObjectInstance>
    <x:Array Type="sys:Double">
      <sys:Double>0.01</sys:Double>
      <sys:Double>0.02</sys:Double>
      <sys:Double>0.03</sys:Double>
      <sys:Double>0.04</sys:Double>
    </x:Array>
  </ObjectDataProvider.ObjectInstance>
</ObjectDataProvider>

<!-- Page content -->
<ComboBox ItemsSource="{Binding Source={StaticResource Incs2}}" />

EDIT: I've also found that if I move the x:Array resource to the top of my Resources section, before any other resource declaration, I can use your original ItemsSource="{StaticResource ...}" and I no longer get the exception (or need the ObjectDataProvider). This would seem to be a bug in WPF.

itowlson
I'm using VS2008, .NET 3.5 SP1 as well.
Dave
I'll give this a shot tomorrow when I need to put my formatter back in, thanks!
Dave
Do you have any good references that discuss when you should use ItemsSource="{StaticResource Incs2}" vs. ItemsSource="{Binding Source={StaticResource Incs2}}"? That markup is very foreign to me right now.
Dave
your ObjectDataProvider seems to do the trick, but the XAML databinding syntax is something I don't understand yet.
Dave
Those two should be the same (because a Binding with a Source and no Path returns the object itself, i.e. the array, which is the same as what the SR returns): as I said, the first one not working appears to be a bug in WPF. For XAML data binding syntax, MSDN is a pretty good place to start: see http://msdn.microsoft.com/en-us/library/ms742451.aspx, http://msdn.microsoft.com/en-us/library/ms752347.aspx and http://msdn.microsoft.com/en-us/library/ms743643.aspx.
itowlson