0

I wish to do this using only XAML - How to add an additional RadComboBoxItem to data bound RadComboBox? - is this possible ?

I have an observable collection that is populating a combo box.

                <telerik:RadComboBox Grid.Row="0" ItemsSource="{Binding ListOfNumbers}" DisplayMemberPath="Name" EmptyText="All" SelectedItem="{Binding SelectedNumber, Mode=TwoWay}"/>

As you can see, at the moment I have the EmptyText property set to "All". This works perfectly, in the method where the value of this combobox is used, I check it, if it is == "", if it is, then it retrieves all data. This is what happens when I first open my view, with nothing selected.

The problem is, when I select a number, I can no longer select "All", which is quite annoying as I would like to be able to.

How can I add this choice to my comboBox and make it save a value of "" to my dependency property, SelectedNumber ?

Community
  • 1
  • 1
Simon Kiely
  • 5,880
  • 28
  • 94
  • 180
  • Can't you create a composite collection ? (http://stackoverflow.com/questions/777048/merged-observablecollection) – Kek Jul 02 '12 at 14:39
  • @Kek That is a possibility, but to me it seems a little more hacky than adding a column in my XAML. – Simon Kiely Jul 02 '12 at 14:42
  • Can you provide a code for the class of which you have created the collection for, if it is not too big? – Shakti Prakash Singh Jul 02 '12 at 14:49
  • What is ListOfNumbers ? a list of a class Number ? I don't understand what you expect All to be binded to. You say "", do you mean null or do you have a way to convert a string to a number ? – Kek Jul 02 '12 at 14:49
  • ListOfNumbers is simply an ObservableCollection of Integers, or the purposes of this let's just assume they are hardcoded. All I want to do is add kind of functionality to my ComboBox; and make this item the default selected item. It is difficult to see how, however! – Simon Kiely Jul 02 '12 at 14:53
  • http://stackoverflow.com/questions/6551473/how-to-add-an-additional-radcomboboxitem-to-data-bound-radcombobox is exactly what I wish to do - but in XAML. – Simon Kiely Jul 02 '12 at 14:55

1 Answers1

1

Something like this should work:

<telerik:RadComboBox.ItemsSource>
    <CompositeCollection>
      <CollectionContainer
        Collection="{Binding ListOfNumbers}" />

      <telerik:RadComboBoxItem Name="Default" Content="All Builds" Value=""></telerik:RadComboBoxItem >
    </CompositeCollection>
  </telerik:RadComboBox.ItemsSource>

No ?

Kek
  • 3,145
  • 2
  • 20
  • 26
  • The type "CompositeCollection" could not be found, also "Value" does not seem to be an attribute for a RadComboBoxItem - there must surely be an alternative, though? – Simon Kiely Jul 02 '12 at 15:06
  • arg! I'm surprised about the CompositeCollection that could not be found : http://msdn.microsoft.com/fr-fr/library/system.windows.data.compositecollection.aspx Regarding the telerik item, I admit I don't use it and the code was based on your comment... you ar using .NET 4.0 ? – Kek Jul 02 '12 at 15:10
  • Yes, .NET 4.0. I, too, am surprised by this. The value code I had above was kind of a projection of what I wish for! I just can't seem to find any way to do it (and it would seem like such a simple/commonly encountered kind of problem!). Thanks for your reply/help :) – Simon Kiely Jul 02 '12 at 15:13
  • Have a look to http://stackoverflow.com/questions/1188402/wpf-combobox-with-empty-item BindingProxy is mentionned, it may help... – Kek Jul 02 '12 at 15:53