2014-03-12 24 views
4

德爾福XE4更新1和Windows 8TListView的組和項目清算和重裝組和項目

當我添加組和項目列表視圖,它們顯示正常後不會出現。當我清除項目和組時,再次添加它們時,什麼都不顯示。當然,這不是預期的行爲?

從DFM:

object lv: TListView 
    Left = 24 
    Top = 20 
    Width = 250 
    Height = 225 
    Columns = < 
    item 
     Caption = 'Model' 
     Width = 180 
    end> 
    GroupView = True 
    ReadOnly = True 
    RowSelect = True 
    TabOrder = 0 
    ViewStyle = vsReport 
end 

代碼:

procedure TForm1.Button1Click(Sender: TObject); 
var 
    LListGroup: TListGroup; 
    LListItem: TListItem; 
begin 
    lv.Items.Clear; 
    lv.Groups.Clear; 

    LListGroup := lv.Groups.Add; 
    LListGroup.Header := 'Ford'; 

    LListItem := lv.Items.Add; 
    LListItem.Caption := 'Escape'; 
    LListItem.GroupID := LListGroup.ID; 

    LListItem := lv.Items.Add; 
    LListItem.Caption := 'F150'; 
    LListItem.GroupID := LListGroup.ID; 

    OutputDebugString(PChar(Format('lv.Groups.Count=%d', [lv.Groups.Count]))); 
end; 

我第一次單擊該按鈕,出現該項目與他們進行分組。第二次,列表視圖是空白的。如果我將清除組的行標註出來,那麼它就可以工作,但是組中的所有這些組都沒有被使用,但是每次都會增加1。

回答

6

與您的代碼的問題是,你正在過TCollectionItemID屬性設置爲TListItemGroupID財產,你必須使用TListGroupGroupID財產。

所以這條線

LListItem.GroupID := LListGroup.ID; //here you are passing a wrong id for the group 

改變

LListItem.GroupID := LListGroup.GroupID; //This is a valid assignment for the GroupID property 
+0

這就是它!完全明顯,但我無法告訴你我看過多少次而沒有看到它。謝謝。 – Pat