문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top