2012-06-19 69 views
0

我在使用gridview和組合框內的一些問題。在GridView中填充組合框

這裏是我的ListView控件的代碼:

<ListView Height="139" HorizontalAlignment="Left" Margin="10,158,0,0" Name="lvAppointment" VerticalAlignment="Top" Width="250" MinWidth="350"> 
<ListView.View> 
    <GridView> 
     <GridViewColumn Header="Appointment" Width="120"> 
      <GridViewColumn.CellTemplate> 
       <DataTemplate> 
        <DatePicker SelectedDate="{Binding Path=Appointment}"/> 
       </DataTemplate> 
      </GridViewColumn.CellTemplate> 
     </GridViewColumn> 
     <GridViewColumn Header="Type" Width="170"> 
      <GridViewColumn.CellTemplate> 
       <DataTemplate> 
        <ComboBox ???/> 
       </DataTemplate> 
      </GridViewColumn.CellTemplate> 
     </GridViewColumn> 
     <GridViewColumn Header="Done" Width="50"> 
      <GridViewColumn.CellTemplate> 
       <DataTemplate> 
        <CheckBox IsChecked="{Binding Path=Done}" IsThreeState="False"/> 
       </DataTemplate> 
      </GridViewColumn.CellTemplate> 
     </GridViewColumn> 
    </GridView> 
</ListView.View> 

我從SQL CE數據庫通過C#與下面的代碼

using (SqlCeCommand sqlCeAppointment = new SqlCeCommand("SELECT appid,appointment,done,apptype.type FROM appointment INNER JOIN apptype ON appointment.refatid = apptype.atid WHERE refeventid = @eventid;", sqlCeConn)) 
    { 
     sqlCeAppointment.Parameters.AddWithValue("@eventid", ((cListEventItem)lvEvent.SelectedItems[0]).id); 
     using (SqlCeDataReader sqlCeAppointmentReader = sqlCeAppointment.ExecuteReader()) 
     { 
      lvAppointment.Items.Clear(); 

      while (sqlCeAppointmentReader.Read()) 
      { 
       lvAppointment.Items.Add(new cListAppointmentItem { id = sqlCeAppointmentReader.GetGuid(sqlCeTerminReader.GetOrdinal("appid")), Appointment = sqlCeAppointmentReader.GetDateTime(sqlCeTerminReader.GetOrdinal("appointment")), Type = sqlCeAppointmentReader.GetString(sqlCeTerminReader.GetOrdinal("type")), Done = sqlCeAppointmentReader.GetByte(sqlCeTerminReader.GetOrdinal("done")) }); 
      } 
     } 
    } 

我可以popluating列表突出顯示列表。但我想要「類型」是一個組合框,所以用戶可以選擇適當的約會類型(其約會連接到一個事件的列表)。這個組合框應該填充sql數據庫(apptype)表內的數據。該表格不是靜態的,用戶可以從該列表添加和刪除項目。

我嘗試了幾種方法,我通過谷歌發現,但失敗了。我想我有問題了解如何工作/應該工作。

我希望有人能幫助我:(

在此先感謝

回答

0

我建議讓在MainViewModel任命類型的列表,然後將ComboBoxItemsSource綁定到該列表並綁定SelectedItem到AppointmentItem類型。

<UserControl x:Name="TheUserControl"> 
    <ListView> 
     <GridView> 
      <GridViewColumn Header="Type" Width="170"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding ElementName=TheUserControl, Path=AppointmentTypes}" 
            SelectedItem="{Binding Path=Type}"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView> 
</UserControl> 
0

您可以通過定義類型的ObservableCollection的屬性做到這一點。 如果收集的數據類型爲字符串那麼你可以將它定義爲:

public ObservableCollection<string> Types 
     { 
      get 
      { 
       ObservableCollection<string> _types = new ObservableCollection<string>(); 
       _types.Add("value1"); 
       _types.Add("value2"); 
       _types.Add("value3"); 
       return _types; 
      } 

     } 

在你的XAML可以綁定它:

<ComboBox VerticalAlignment="Top" HorizontalAlignment="Left" ItemsSource="{Binding Path=Types}" SelectedItem="{Binding MainViewModel.Type,Mode=TwoWay}" SelectedValue="{Binding SelectedValue}" Width="300" ></ComboBox>