2010-11-19 117 views
0

我從API中收集dictionary <string, string>在列表視圖中顯示動態數據,gridview

我在網格視圖名稱和值,以顯示我的WPF的形​​式對這個數據作爲兩列

<ListView Name="LstCustomProperties" ItemsSource="{Binding CustomPropertyTable}"> 
    <ListView.View> 
    <GridView> 
     <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Key}" /> 
     <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}" /> 
    </GridView> 
    </ListView.View> 
</ListView> 

我也有形式上的兩個按鈕來添加按鈕添加新的項目或刪除刪除任何項目。當用戶單擊確定時,字典將根據當前名稱,值列表中的值對進行更新。我沒有得到如何添加和修改列表視圖中的當前數據或使用任何其他控件。

回答

1

我寧願在這裏使用ObservableCollection。所以當你收集任何物品時,UI會自動刷新。

請看下面的例子:

public class CustomDictionary 
{ 
    public string Key { get; set; } 
    public string Value { get; set; } 

    public CustomDictionary(string key, string value) 
    { 
     this.Key = key; 
     this.Value = value; 
    } 
} 

public class CustomDictionaryCollection : ObservableCollection<CustomDictionary> 
{ 

} 


public class MyData 
{ 
    public CustomDictionaryCollection CustomPropertyTable { get; set; } 

    public MyData() 
    { 
     this.CustomPropertyTable.Add(new CustomDictionary("myKey", "myValue")); 
    } 
} 

現在當你在CustomPropertyTable添加任何東西,在ListView將自動得到更新。

希望這有助於

+0

我不想在對象直接添加它會在電網和當其用戶點擊將被保存到對象和API將更新的數據存儲。 – Mohit 2010-11-19 08:28:57

+0

抱歉,因爲我在度假時太晚了。 我還想讓用戶從列表視圖中編輯值成員,我們將非常感謝這方面的任何幫助 – Mohit 2010-11-30 12:21:40