0

i have a ListView in wpf

<ListView Name="listArea">
    <ListView.View>
        <GridView>
            <GridViewColumn x:Name="colName" Header="نام تحویلدار" Width="150" DisplayMemberBinding="{Binding Path=name}"/>
            <GridViewColumn x:Name="colComboBox" Header="منطقه" Width="120" DisplayMemberBinding="{Binding Path=cb}"/>
            </GridView>
     </ListView.View>
 </ListView>

i want add item to listview. first column is text and secound is comboBox.

foreach(personel ptahvildar in STATICS.db.personels.Where(q=>q.postCode==2))
{
    ListViewItem item = new ListViewItem();
    ComboBox cbox = new ComboBox();
    cbox.ItemsSource = STATICS.db.personels.Where(q => q.postCode == 2);
    cbox.DisplayMemberPath = "name";
    cbox.SelectedItem = ptahvildar;
    item.Content = new { name = ptahvildar.name, cb = cbox };
    listArea.Items.Add(item);
}

but the result is like this

result

why my comboBox not show correctly ?

1 Answer 1

0

You should modify the ListView.itemTemplate property and add a data template.

Using an item template with data template you can add checkboxes, comboboxes, textboxes, buttons, etc. for every line item in your list view. Here is an example from an answer here in SO.

<ListView ItemsSource="{Binding Links}" x:Name="ListView1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border>
                    <Button Command="{Binding ElementName=ListView1, Path=DataContext.GetOddsCommand}" CommandParameter="{Binding}">
                         <TextBlock Text="{Binding}" />
                    </Button>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate> 
</ListView>

Except you only have to use a combobox instead of a button and use proper bindings..

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

2 Comments

each combobox.selecteditem Depends to another column value ... how i pass another column value to code behind and change selecteditem of comboBox ?
The CommandParameter is responsible for passing values to the Command. You can pass the specific field (column) from your item to the event. As I've said you have to use proper bindings as I only extracted the sample from some answer here in SO.

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.