2011-12-21 26 views
0

我有一個CommentsData類,它用於加載,操作和保存DataGrid中的值。我想讓網格中的狀態欄顯示爲下拉菜單。註釋值只需填充一次。我嘗試過很多變體,但這不起作用。組合是空白的。我需要能夠填充組合中的值,並且當選擇更改時,值應該保留在那裏並且不會消失。DataGridComboBoxColumn自定義類的值

這裏是XAML中的網格(更新2)

下面是CommentData類(更新2)

public class CommentsData 
{ 

    public string Author { get; set; } 
    public string Status { get; set; } 
    public string Comment { get; set; } 
    public string Username { get; set; } 

    public ObservableCollection<StatusValue> UserValues { get; set; } 

    public CommentsData() 
    { 
     UserValues = new ObservableCollection<StatusValue>(); 

     UserValues.Add(new StatusValue("New")); 
     UserValues.Add(new StatusValue("Open")); 
     UserValues.Add(new StatusValue("ReOpen")); 
     UserValues.Add(new StatusValue("Closed")); 

    } 

} 

public class StatusValue 
{ 
    public string UserStatus { get; set; } 

    public StatusValue (string value) 
    { 
     UserStatus = value; 
    } 
} 

在這裏,代碼是代碼,其中評論列表已初始化

private List<CommentsData> _commentsList; 

private void InitializeObjects() 
{ 
     _commentsList = new List<CommentsData>(); 
     grdComments.ItemsSource = _commentsList; 

} 

上面的代碼正在感謝所有反饋

回答

1

由於MSDN上的文章指出約DataGridComboBoxColumn來填充下拉列表中,您必須首先使用下列選項之一設置組合框ItemsSource屬性:

  • 靜態資源。
  • x:靜態代碼實體。
  • ComboBoxItem類型的內聯集合。

如果你要綁定ComboBox.ItemsSource到對象屬性,它`更容易使用DataGridTemplateColumn這樣的:

<DataGridTemplateColumn Header="Status"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
       <ComboBox ItemsSource="{Binding UserValues}" DisplayMemberPath="UserStatus" SelectedValuePath="UserStatus" SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 
+0

列表被填充和選擇也但是當我嘗試從DataGrid中獲取數據以保存它時,狀態值始終保持爲New,並且不反映更改的值 – CodeMe 2011-12-22 07:33:00

+0

@CodeMe,我忘記將UpdateSourceTrigger添加到SelectedValue Binding。我正在更新代碼,現在應該工作。 – icebat 2011-12-22 07:51:13

+0

'UpdateSourceTrigger'是否需要'INotifyPropertyChanged'實現? – CodeMe 2011-12-22 08:22:50

0

以下是我會用,而不是你DataGridComboboxColumn:

<DataGridTemplateColumn> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <ComboBox ItemsSource="{Binding UserValues}" SelectedItem="{Binding Status}" DisplayMemberPath="UserStatus" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

CommentsData.Status也應該是StatusValue類型,而不是string,以便您可以綁定SelectedItem

+0

讓'CommentsData.Status'成爲一個字符串是完美的。我通常建議人們使用'SelectedValue'來綁定選擇的項目而不是'SelectedItem',因爲'SelectedItem'是通過引用的,這意味着如果它沒有指向內存中與ItemsSource中的版本完全相同的引用,將不會被選中。 – Rachel 2011-12-21 15:14:17

+0

我推薦完全使用'SelectedItem'因爲'StatusValue'類型的'CommentsData.Status'。我個人推薦使用'SelectedItem',這樣所選項目**就是綁定到項目源的列表項目之一,特別是在使用MVVM模式時(您希望選定項目實例具有與來自數據源的項目)。 – ken2k 2011-12-21 15:42:32

+0

我想每個人都有自己的喜好。我通常會進行一次數據庫調用以獲取ComboBox值的列表,然後進行第二次數據庫調用以獲取記錄。如果綁定了'SelectedItem',我必須確保第二次獲取記錄的數據庫調用會附加從第一次數據庫調用中獲得的ComboBox項目,並且不會從數據庫創建或加載它自己的副本。 ORM系統尤其如此,實體框架會自動創建相關項目。 – Rachel 2011-12-21 15:58:14

1

有,我看到從您的代碼

首先缺少了一些東西,你的類沒有實現INotifyPropertyChanged。這意味着,如果屬性在CommentData上更改,它不會告知UI已更改,因此UI不會更新以顯示新值。

其次,你告訴你的ComboBox,有一個叫Status在每個項目上的財產,並把它作爲ComboBoxItem.Value,但是這個屬性上不存在StatusValue。將其更改爲UserStatus,這是StatusValue上的有效財產。

SelectedValuePath="UserStatus" 

最後,你真的不應該重新創建每個項目上的組合框項目。相反,請在ViewModel層次結構的更上方創建集合,或將其作爲靜態資源。

例如,如果其中包含您的CommentsData集合類中還含有的StatusValues您的收藏,你可以使用一個RelativeSource綁定綁定到它是這樣的:

ItemsSource="{Binding 
    RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, 
    Path=DataContext.UserValues}" 
+0

根據@Rachel建議'INotifyPropertyChanged'已經實現,'SelectedValuePath'也被更新並且集合已經移動到維護註釋列表的位置。現在組合列表變空了我已經更新了代碼,以便您可以查看。 – CodeMe 2011-12-22 08:03:32

+0

@CodeMe我沒有看到你的'DataGrid.ItemsSource'綁定到'CommentList'。 DataGrid的'DataContext'是什麼?它應該是任何包含'CommentList'和'UserValues'列表的類,但是如果是這種情況,當我在DataGrid – Rachel 2011-12-22 13:02:36

+0

上看到ItemsSource =「{Binding CommentList}」'抱歉忘了提及它時,我已經更新代碼它的'grdComments.ItemsSource = _commentsList;'。包含兩個集合的類是我的窗口類 – CodeMe 2011-12-22 14:29:43