2012-07-31 141 views
0

我有一個ListView看起來像這樣的DataTemplate中:綁定一個TextBox到ComboBox的選定值在ListView

enter image description here

組合框被綁定到類型TfsTask的對象的一個​​ObservableCollection 。當用戶改變選擇時,我希望在同一行上的控件被包含在組合框中所選項目中的數據填充。例如,假設任務「測試」具有特定的持續時間值和另一個剩餘值。如果用戶在組合框中選擇此任務,我想用這些值填充行上的控件。

下面是問題:如何將其他列中的控件綁定到組合框的選定項目。當直接綁定到控件時,可以使用它的ElementName來實現,但是如何在DataTemplate中實現?

這裏是我如何在XAML中定義我的ListView。我只把任務組合框和時間文本框作爲一個例子

<ListView MinHeight="100" Name="m_taskList"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Width="140" Header="Task" > 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <Grid HorizontalAlignment="Stretch"> 
          <ComboBox Name="m_taskName" DisplayMemberPath="Name" SelectedValue="{Binding Path=TaskId, Mode=TwoWay}" SelectedValuePath="Id" ItemsSource="{Binding ElementName=This, Path=TfsTasks}/> 
         </Grid> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn Width="140" Header="Duration" > 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <Grid > 
          <TextBox Text="{Binding ????}" /> 
         </Grid> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <!-- .... --> 
     </GridView> 
    </ListView.View> 
</ListView> 
+0

我最近調查ListView控件的使用出於類似的目的,並確定一個DataGrid是最好這個情況。谷歌,例如,「datagridcomboboxcolumn」..讓事情顯着簡化IMO。 – Califf 2012-11-30 22:45:26

回答

1

它的工作,但你必須在你的目標在這個解決方案中添加屬性。

試試這個:

XAML文件:

<Window x:Class="ListViewCombo.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ListView MinHeight="100" Name="m_taskList" ItemsSource="{Binding Path=MyItems}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Width="140" Header="Task" > 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <Grid HorizontalAlignment="Stretch"> 
            <ComboBox Name="m_taskName" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedItem}" SelectedValuePath="ID" ItemsSource="{Binding Path=Items}" /> 
           </Grid> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn Width="140" Header="Duration" > 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <Grid > 
          <TextBox MinWidth="150" Text="{Binding Path=SelectedItem.Duration}" /> 
         </Grid> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn>     
      </GridView> 
     </ListView.View> 
    </ListView> 
</Grid> 
</Window> 

視圖模型文件:

using System.Collections.ObjectModel; 
using Microsoft.Practices.Prism.ViewModel; 

namespace ListViewCombo 
{ 
    class MainViewModel : NotificationObject 
    { 
     public MainViewModel() 
     { 
      for (int i = 0; i < 3; i++) 
      { 
       ObservableCollection<Task> Source = new ObservableCollection<Task>(); 
       for (int j = 0; j < 5; j++) 
       { 
        Source.Add(new Task { ID = i, Name = "Name_" + i, Duration = (i + 2) * 6 + (3 * j) }); 
       } 
       MyItems.Add(new TfsTask { ID = i, Items = Source }); 
      }       
     } 

     private ObservableCollection<TfsTask> _myItems = new ObservableCollection<TfsTask>(); 
     public ObservableCollection<TfsTask> MyItems 
     { 
      get { return _myItems; } 
      set { _myItems = value; RaisePropertyChanged(() => MyItems); } 
     }  
    } 

    public class Task 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public int Duration { get; set; } 
    } 

    public class TfsTask 
    { 
     public int ID { get; set; } 
     public ObservableCollection<Task> Items { get; set; } 
     public Task SelectedItem { get; set; } 
    } 
}