1

I am unable to find a solution for this.. I have a ListBox, whose DataTemplate has a ComboBox. DataBinding is in place, this is kind of collection of collections scenario. I want to preprend a 'Select One Item' to all ComboBoxes. How do I do that?

EDIT: Really not sure why you would need code/xaml for above question. But below anyway:

<Resources>
<ResourceDictionary>
            <DataTemplate x:Key="CategoriesDataTemplate">
                <StackPanel Orientation="vertical">
                    <TextBlock Text="{Binding Path=CategoryName}"></TextBlock>
                    <ComboBox ItemsSource="{Binding Path=Products}" Background="Transparent" SelectedValuePath="ProductId" DisplayMemberPath="ProductName">
                    </ComboBox>
                </StackPanel>
            </DataTemplate>
</ResourceDictionary>
</Resources>
.....
<Grid..>
                <ListBox ItemsSource="{Binding Categories}" ItemTemplate="{DynamicResource CategoriesDataTemplate}">
</Grid>

For each category, I will display the category name and a combobox of its products below. User can select one product per category. For each such combobox, I want the first item to be "Select a Product" or something such. Note: I am looking to see if there is a way to do it WITHOUT pre-pending a item to each of my Products collection in each Category(I do not wish to mess with the source collections if possible). Some sort of event handler approach?

Brian
  • 1,337
  • 5
  • 17
  • 34
  • 1
    Code, Xaml, anything? – UIlrvnd Nov 01 '13 at 20:31
  • @StefanDenchev ok I added partial Xaml now. Your turn :) – Brian Nov 01 '13 at 20:59
  • "Really not sure why you would need code/xaml for above question.": Because it aids comprehension... If i understand correctly, [this](http://stackoverflow.com/questions/18113293/display-select-an-item-in-combobox-when-selecteditem-is-null) should suffice... Hah, two other "Brian"s participated on that question... – UIlrvnd Nov 01 '13 at 23:01
  • @StefanDenchev I guess my edit really didn't help. if I understand your link correctly, it talks about showing text, not 'prepending' an extra item ( I may be wrong, I am new to wpf). I want to pre-pend an item, not just set the text property. I managed a solution as below – Brian Nov 01 '13 at 23:36
  • Yes, i thought it would be enough to just show it instead of the null field in the beginning... Glad you figured it out. – UIlrvnd Nov 01 '13 at 23:43

1 Answers1

0

After further digging, got a solution combining:

hbarc's suggestion and Shimmy's solution and kmatyaszek's answer. Trick is to use CompositeCollection, so we can use both static and dynamic items for the ComboBox

My ComboBox looks something like this now (I couldn't get it to work with StackPanel resources. I am new to wpf someone please comment on the StackPanel resources approach.):

                <StackPanel.Resources>
                    <CollectionViewSource Source="{Binding Path=Products}" x:Key="options"/>
                    <!--not used. doesn't seem to be working when used DummyOption is a property in ViewModel-->
                    <CollectionViewSource Source="{Binding DummyOption}" x:Key="dummyOption"/>
                </StackPanel.Resources>

                <ComboBox Background="Transparent" SelectedValuePath="ProductId" DisplayMemberPath="ProductName" SelectedIndex="0">
                    <ComboBox.ItemsSource>
                        <CompositeCollection>
                            <!--works-->
                            <models:Product ProductName="Select" ProductId="{x:Static sys:Guid.Empty}" .../>
                            <!--notworking-->
                            <!--<ComboBoxItem Content="{Binding dummyOption}" />-->
<!--notworking-->
<!--<ComboBoxItem Content="{Binding DummyOption}" />-->
                            <!--notworking-->
                            <!--<ComboBoxItem Content="{Binding Source={StaticResource ResourceKey=dummyOption}}" />-->
                            <CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=Products}}" />
                        </CompositeCollection>
                    </ComboBox.ItemsSource>    
                </ComboBox>
Community
  • 1
  • 1
Brian
  • 1,337
  • 5
  • 17
  • 34