开发者

Display dynamic data in listview, gridview

I recives a dictionary <string, string> from an API.

I have to show this data on my WPF form in in grid view as Name and Value as two columns

<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>

I also have two buttons on the form t开发者_运维技巧o add button to add new item or delete to delete any item. When user clicks ok the dictionary will get update according to current Name, value pair in the listview. I am not getting how to add and modify the current data in listview or shud I use any other control.


I would prefer using ObservableCollection here. so when you insert / update / delete any item from collection, the UI will automatically get refreshed.

see the following example:

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"));
    }
}

now when you add anything in the CustomPropertyTable, the ListView will automatically get updated.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜