0

I have this code:

<ComboBox Name="cbxWorkers" HorizontalContentAlignment="Right"
          ItemsSource="{Binding Workers}">
   <ComboBoxItem IsSelected="True" Content="Select" />
      <ComboBox.ItemTemplate>
         <DataTemplate>
            <ComboBoxItem Content="{Binding LastName}" />
         </DataTemplate>
      </ComboBox.ItemTemplate>
</ComboBox>

Everything works fine except the second line. It gives me a runtime exception: Items collection must be empty before using ItemsSource.

How can I deal with that so I will get also all the Workers, and also the item - "Select" as the first item of the combobox?

Thanks a lot :)

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116

2 Answers2

0

You can't have two sources. You'd have to specify in code from the ItemsSource which item you want selected.

<ComboBox Name="cbxWorkers" HorizontalContentAlignment="Right" ItemsSource="{Binding Workers}">
<ComboBox.ItemTemplate>
    <DataTemplate>
        <ComboBoxItem Content="{Binding LastName}" IsSelected="{Binding isSelected}" />
    </DataTemplate>
</ComboBox.ItemTemplate>

You can do this, or you can just make an extra first Item in C#/VB and make sure it's selected.

0

You can do this with a CompositeCollection:

<ComboBox x:Name="cbxWorkers" SelectedIndex="0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=LastName}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem Content="Select" />
            <CollectionContainer Collection="{Binding Workers}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

Note: you need to set the SelectedIndex="0" on the ComboBox, because when the ComboBoxItem is in the ItemsSource, its IsSelected property won't set the selection on the ComboBox.

Edit concerning CollectionContainer

As @H.B. pointed out, the Binding on CollectionContainer won't work this way. You have a couple of options. They are spelled out for you by this CodeProject article, so I won't repeat them here. The one method that doesn't mention is the newish (as of .NET 4) x:Reference option. It would be used like this:

<CollectionContainer Collection="{Binding DataContext.Workers, Source={x:Reference cbxWorkers}}" />
Abe Heidebrecht
  • 30,090
  • 7
  • 62
  • 66
  • Why would it not select? After all the `DataTemplate` will be ignored. Also commonly [those bindings won't work](http://stackoverflow.com/q/6446699/546730). – H.B. Aug 17 '13 at 17:39
  • I have no idea, but it didn't in Kaxaml, but worked fine when setting the `SelectedIndex`. It probably has something to do with timing (I bet setting it after the view has loaded would work). I'm going to update the answer about the second point, however, as you are definitely right about it. – Abe Heidebrecht Aug 17 '13 at 21:34
  • @H.B. also, I had no idea that x:reference was working in WPF now. I just assumed the documentation for it would be up to date, and never tried it. That is going to allow me to delete some code. So sad that that makes me happy... – Abe Heidebrecht Aug 17 '13 at 21:52
  • Well, it's been around long enough for me to have never used anything else. Though it's still kind of a pain to this back-referencing just to get the DataContext... – H.B. Aug 17 '13 at 23:23