tags:

views:

241

answers:

2

I have class Products with unknown numbers of properties such as "Name", "Price" and so on. I retreive them only when parsing some txt file.

How should I:

  1. write a Product class to emplement dynimicly adding Properies
  2. Bind this collection of Products with unknonwn numbers of properties to ListView in XAML (or should I build in code behind after parsing txt file?)
+1  A: 
  1. You can use a Dictionary<string, object> to store the property names and associated values.

  2. You need to generate the columns dynamically in code-behind, based on the keys in the dictionary. You can bind to a specific item of a dictionary using a binding path like "Properties[SomeKey]" (assuming your dictionary is called Properties).

Thomas Levesque
A: 

Thank you Thomas for a quick answer. - I make Dictionary where object is List<string> - Im trying to Bind to GridView but no result just this Test emlementation works but of course this not that I want what I should write in:

  • Grid1.DataContext
  • listView1.ItemsSource
  • gvc.DisplayMemberBinding

_

enter code here

_importView = new Views.ImportBomView();
        _importView.Grid1.DataContext = _dynimicList;
        _importView.listView1.ItemsSource = (IEnumerable)_dynimicList.PropsDict["Value"];
        foreach (var item in _dynimicList.PropsDict.Keys)
        {
            GridViewColumn gvc = new GridViewColumn();
            gvc.DisplayMemberBinding = new Binding();
            gvc.Header = item;
            gvc.Width = 100;

            _importView.gridView1.Columns.Add(gvc);
        }

        _importView.Show();
Janus