2014-09-27 71 views
1

(我需要澄清的是,我可以管理的一兩個C#或VB的解決方案來解決這個問題)Telerik的,更新RadListControl項目問題

前段時間我問this other question如何選擇項目或範圍在ListBox沒有讓控制跳到那個位置,這個問題現在是類似的,我只想執行與該問題相同的事情,但與telerik RadListControl

我有一個RadListControl我想用它來列出系統中的運行進程(條件是不超過1個具有相同名稱的進程),那件事就完成了,我試圖更新該列表每2秒(首先確定是否存在更改以更新當前列表), 我正在保存控件的SelectedItems屬性,以在更新列表後恢復RadListControl中的選定項目,這是清除後的問題控件的項目使用Items.Clear方法滾動條移動到控件的頂部,我需要一次又一次滾動到所需的位置。 我想在更新控件中的項目之後保持當前位置,就像Windows TaskManager所做的那樣。

我也嘗試設置收集od RadDataItem作爲控制DataSource,但是然後每個項目的Image屬性是空的(我不知道爲什麼)。

這是問題的一個示範:

enter image description here

正如你所看到的,當我(在GIF左側的窗口)運行一個新的進程的進程列表被移動到頂部,當我關閉該過程時也是如此。

我不知道還有什麼嘗試,這裏是代碼的相關部分:

Private Sub Timer_RefreshProcessList_Tick(ByVal sender As Object, ByVal e As EventArgs) _ 
Handles Timer_RefreshProcessList.Tick 

    ' Processes that shouldn't be listed. 
    Dim BlackListedProcesses As String() = 
     { 
      My.Application.Info.AssemblyName, 
      "Idle", 
      "System", 
      "audiodg" 
     } 

    ' Get the runing processes. 
    Dim Processes As Process() = Process.GetProcesses 

    ' Filter the processes by its name 
    ' then set the RadListDataItem items containing the names and the process icons. 
    Dim ProcessItems As IEnumerable(Of RadListDataItem) = 
     (From proc As Process In Processes 
     Where Not BlackListedProcesses.Contains(proc.ProcessName) 
     Order By proc.ProcessName Ascending). 
     GroupBy(Function(proc As Process) proc.ProcessName). 
     Select(Function(procs As IGrouping(Of String, Process)) 

        If Not procs.First.HasExited Then 
         Try 
          Return New RadListDataItem With 
            { 
            .Active = False, 
            .Text = String.Format("{0}.exe", procs.First.ProcessName), 
            .Image = ResizeImage(Icon.ExtractAssociatedIcon(procs.First.MainModule.FileName).ToBitmap, 
                 Width:=16, Height:=16) 
            } 

         Catch ex As Exception 
          Return Nothing 
         End Try 

        Else 
         Return Nothing 
        End If 

       End Function) 

    ' If the RadListControl does not contain any item then... 
    If Me.RadListControl_ProcessList.Items.Count = 0 Then 

     With Me.RadListControl_ProcessList 
      .BeginUpdate() 
      .Items.AddRange(ProcessItems) ' Add the RadListDataItems for first time. 
      .EndUpdate() 
     End With 

     Exit Sub 

    End If 

    ' If RadListDataItems count is not equal than the runing process list count then... 
    If Me.RadListControl_ProcessList.Items.Count <> ProcessItems.Count Then 

     ' Save the current selected items. 
     Dim SelectedItems As IEnumerable(Of String) = 
      From Item As RadListDataItem 
      In Me.RadListControl_ProcessList.SelectedItems 
      Select Item.Text 

     ' For Each ctrl As RadListDataItem In ProcessItems 
     '  ctrl.Dispose() 
     ' Next 

     With Me.RadListControl_ProcessList 

      ' .AutoScroll = False 
      ' .SuspendSelectionEvents = True 
      ' .SuspendItemsChangeEvents = True 
      ' .SuspendLayout() 
      ' .BeginUpdate() 
      .Items.Clear() ' Clear the current RadListDataItems 
      .Items.AddRange(ProcessItems) ' Add the new RadListDataItems. 
      ' .EndUpdate() 
      ' .ResumeLayout() 

     End With 

     ' Restore the selected item(s). 
     For Each Item As RadListDataItem In Me.RadListControl_ProcessList.Items 

      If SelectedItems.Contains(Item.Text) Then 

       Item.Selected = True 
       Item.Active = True 

       With Me.RadListControl_ProcessList 
        ' .ScrollToItem(Item) 
        ' .ListElement.ScrollToItem(Item) 
        ' .ListElement.ScrollToActiveItem() 
       End With 

      End If 

     Next Item 

     With Me.RadListControl_ProcessList 
      ' .AutoScroll = True 
      ' .SuspendSelectionEvents = False 
      ' .SuspendItemsChangeEvents = False 
     End With 

    End If 

End Sub 

回答

1

問題是你清空列表後,SelectedItems列表中不包含任何東西。改用:

Dim SelectedItems As List(Of String) = New List(Of String) 

For Each Item As RadListDataItem In RadListControl_ProcessList.SelectedItems 
    SelectedItems.Add(Item.Text) 
Next 

然後你可以滾動到任何項目。

EDIT(我應該更詳細解釋它。我傾向於這樣做)

是什麼原因造成這種現象是LINQ查詢你正在做來從所選項目文本。它不符合你的想法。例如,它不會在選定的項目中添加任何文本。當您訪問SelectedItems查詢執行:在這一點上,項目被清除

(那些已與SelectedItems有關)並沒有任何反應。你能做些什麼來解決這個問題是使用靜態列表,我建議,或更好的(不改變你的代碼太多):

Dim SelectedItems As IEnumerable(Of String) = 
     (From Item As RadListDataItem 
     In Me.RadListControl_ProcessList.SelectedItems 
     Select Item.Text).ToArray 

都工作

瓦爾特

+0

如果我沒理解好,我只列出了指向'RadControl.SelectedItems'名單,但只是,只有指向它的項目,所以一旦我清除Control.SelectedItem列表我的列表也開始爲空,因爲有任何項目指向,我說得對嗎?,我不明白的是爲什麼要這樣做,因爲它解決了這個問題,我沒有做同樣的事情,但使用Linq ?,對不起我的英語,謝謝你的回答! – ElektroStudios 2014-10-03 15:49:53

+1

@ElektroStudios對不起。見編輯:) – 2014-10-03 16:31:26

1

如果我沒有理解正確地,你想保持滾動條的位置,一旦你重新填充控件中的項目。如果是的話,下面的代碼可以幫助你實現它:

int saveValue = radListControl1.ListElement.Scroller.Scrollbar.Value; 

//rebind 

radListControl1.ListElement.Scroller.Scrollbar.Value = saveValue; 
+0

謝謝你的答案,兩個答案幫助我。 +1 – ElektroStudios 2014-10-03 15:47:06