0

I have a combobox which displays listview on dropdown, I am following MVVM Pattern and i have also set the public property in my Viewmodel and it works fine when i am assigning it to the Label but for Combobox it doesn't seem to rely on my binding. i tried numerous ways but unable to find the issue.

XAML :

 <ComboBox Name="SearchBox" IsEditable="True"  Background="White"  md:HintAssist.Hint="Search MUID"    Grid.Column="1" Margin="5 0 0 0" 
                  Grid.Row="0"  Height="40" Width="400"  HorizontalContentAlignment="Left" HorizontalAlignment="Left" SelectedItem="{Binding ElementName=lstview ,Path=SelectedItem}" >
                                <ComboBoxItem>
                                <ListView x:Name="lstview" ItemsSource="{Binding filterSW}" 
                    SelectedItem="{Binding SelectedMU}" 
                     Height="200" ScrollViewer.VerticalScrollBarVisibility="Visible">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Width="130"  Header="Mu-ID"    />
                                            <GridViewColumn Width="130" Header="MU-Identifier" DisplayMemberBinding="{Binding MU_Identifier}"  />
                                            <GridViewColumn Width="130"  Header="Status" DisplayMemberBinding="{Binding RequestType}" />
                                            <GridViewColumn Width="130" Header="UniqueID"  />
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                                </ComboBoxItem>
                            </ComboBox>

This works fine for me when i am using the public property and accessing its element , i also tried setting text={Binding SelectedMU.MU_Identifier} and selectedvalue but its just not working.

 <Label Grid.Column="3" HorizontalAlignment="Center"  Background="GreenYellow" Content="{Binding SelectedMU.MU_Identifier}"></Label>
1
  • Your binding is it a bit weird for the Combobox. Why are you injecting a ListView into a single ComboBox item? Why have a ComboBox at all? Commented Oct 8, 2017 at 20:48

1 Answer 1

2

It looks like you're trying to show a multi-column list in your ComboBox dropdown instead of the standard list where each item shows just a text line.

To achieve this effect you've placed a ListView inside the dropdown.

Unfortunately, this is just not going to work.

Both ComboBox and ListView descend from Selector which is an abstraction that allows to select an item from a list. This limits the property SelectedItem to one of the items that are contained in the list. If you try to assign to this property any value that it not in the list, the assignment is not going to work and the property will retain the value it had before you did the assignment.

Now, the list could either be specified right inside XAML or provided as a binding to property ItemsSource. You do the binding correctly for the ListView. But for the ComboBox you don't specify that binding. Instead you specify exactly one item of type ComboBoxItem which contains the whole ListBox as its value. So the only value that could be successfully assigned to the SelectedItem property of the ComboBox is that single ComboBoxItem. But your binding is never going to assign that value, that's why the ComboBox never shows anything when closed.

When it's open it does show the single item which contain the ListView but this is just an optical effect. The data binging is not going to work. The reason why it works for the Label is because the Label is not constrained and can show anything that the ListView tells it to show.

You can synchronize the ListView and the ComboBox only when both controls have the same bindings for both ItemsSource and SelectedItem properties. But in this case you won't be able to place the ListView inside the dropdown.

The closest you can get to what you want is by customizing the ComboBox's template as described in https://zamjad.wordpress.com/2012/08/15/multi-columns-combo-box, for example. What this won't give you compared to ListView is the column headers. Also, the columns will be evenly spaced inside the dropdown but this is what you have in your ListView anyway.

If you want to auto-size them, you'd need to add Width="Auto" SharedSizeGroup="cN" to each ColumnDefinition where "cN" should have the column number instead of N to make them unique within the Grid and add Grid.IsSharedSizeScope="True" to the <ComboBox >

That's a lot of trouble for something that one would expect to be much simpler, but, unfortunately, you cannot place a ListView inside the ComboBox's template, that's a limitation of how the base class Selector works with its items list.

There are other options if you are open to consider 3rd party control libraries. I worked with Syncfusion, they have SfMultiColumnDropDown which does what you want. I'm pretty sure other popular libraries have similar controls as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your insight. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.