Ravi Patel
Ravi Patel

Reputation: 2191

Binding combobox inside listview to a collection

Here is my object model

private class ShellItem
{
    public string Name { get; set; }

    public string Command { get; set; }

    public List<ShellMenu> ShellMenus { get; private set; }

    public ShellItem()
    {
        ShellMenus = new List<ShellMenu>();
    }
}


public class ShellMenu
{
    public string ExtName;

    public string KeyName;

    public string FileType;

    public string MenuName { get; set; }

    public string Command { get; set; }

}

Here is my xaml.

<ListView Name="listViewRightClick" HorizontalAlignment="Stretch" Height="Auto" Margin="10,10,26.667,107.667" VerticalAlignment="Stretch" Width="Auto">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="200" Header="Name" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Width="600" Header="Command" DisplayMemberBinding="{Binding Command}"/>
                    <GridViewColumn Width="200" Header="FileTypes">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding ShellMenus}"
                                          SelectedValue="{Binding MenuName}"
                                          Margin="-6, 0, -6, 0"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

My problem is instead of showing MenuName of shellMenu it shows MyNamespace.ShellMenu on every item of combobox.

Upvotes: 0

Views: 3576

Answers (2)

pchajer
pchajer

Reputation: 1584

You need to specify the DisplayMemberPath in the ComboBox.

<ComboBox 
    ItemsSource="{Binding ShellMenus}"
    SelectedValue="{Binding MenuName}"
    DisplayMemberPath="MenuName"
    Margin="-6, 0, -6, 0"/> 

Upvotes: 0

brunnerh
brunnerh

Reputation: 185280

SelectedValue is about selection only, use DisplayMemberPath for the display.

DisplayMemberPath="MenuName"

Upvotes: 4

Related Questions