2013-10-15 64 views
0

對於那些想要知道的人來說,這是一個自穩定算法的工具。如何綁定ListView以查看一組對象的特定屬性?

說我有幾類,AlgorithmRulePredicateActionGraph,並Node,如此定義:

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace SMP { 
    class Algorithm { 
     public List<Rule> Rules { get; set; } 

     public Algorithm() { 
      Rules = new List<Rule>(); 
     } 
    } 

    class Rule { 
     public Predicate Predicate { get; set; } 
     public Action Action { get; set; } 
    } 

    class Predicate { 
     public string Description { get; set; } 
     public string Name { get; set; } 
     public string Expression { get; set; } 
    } 

    class Action { 
     public string Description { get; set; } 
     public string Name { get; set; } 
     public string Expression { get; set; } 
    } 
} 

我想連接一個兩列ListView將顯示Predicate.NameAction.Name對於某些Algorithm.Rules中的每個元素。

注意我用了這個帖子下面的變量名:

ListView algorithm_view; 
Algorithm algorithm 

我知道我必須設置的algorithm_viewDataContextAlgorithm實例與algorithm_view.DataContext = algorithm,但我不知道該怎麼在XAML中表示一個像這樣的集合的綁定。

如果它有助於圖片吧,這裏是界面的截圖:

enter image description here

回答

2

如果您DataContext設置正確的觀點,那麼你可以在你的Rules屬性綁定到ListView.ItemsSource財產。然後,GridViewColumn中的Binding將查找集合類型Rules的類,因此我們可以直接在Bind那裏找到那些屬性。您可以從MSDN上的ListView Class頁面瞭解更多信息。你的XAML應該是這個樣子:

<ListView ItemsSource="{Binding Rules}"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn DisplayMemberBinding="{Binding Predicate}" 
       Header="Predicate" /> 
      <GridViewColumn DisplayMemberBinding="{Binding Action}" 
       Header="Action" /> 
     </GridView> 
    </ListView.View> 
</ListView> 

順便說一句,使用WPF時,明智的做法是實現數據類型的類INotifyPropertyChanged interface,如果你想你的用戶界面和模型時屬性更改進行更新。您也應該使用ObservableCollection<T> collection s也是出於同樣的原因。

最後一點......您的標題目前有點誤導,因爲.NET中有一個類叫做Tuple,您的問題與它無關。

+0

感謝您的回答!我會用'ObservableCollection '而不是'List '嗎? –

+0

在WPF應用程序中,這通常是可取的。 – Sheridan

+0

甜。這需要對我的軟件進行一些重新構建,但是一旦我已經完成所有工作,我會接受你的答案---這似乎是我錯過的關鍵信息,但要做到這一點,我會需要一點時間。謝謝! –

相關問題