2016-07-08 114 views
0

编程的ListView我有一个ListView WPF中添加组合框在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> 

我想补充项目列表视图。第一列是文本,secound是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); 
} 

但结果却是这样

result

为什么我的组合框无法正确显示?

回答

0

您应修改ListView.itemTemplate property并添加数据模板。

对数据模板使用项目模板,您可以为列表视图中的每个行项目添加复选框,组合框,文本框,按钮等。 Here就是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> 

除非你只需要使用一个组合框,而不是一个按钮,并使用合适的绑定..

+0

每个combobox.selecteditem依赖于另一列的值...我如何通过另一列值后面的代码和改变comboBox selecteditem? – abbas

+0

CommandParameter负责将值传递给Command。您可以将项目中的特定字段(列)传递给事件。正如我所说的,你必须使用适当的绑定,因为我只是从SO中的一些答案中提取样本。 –