0

I'm trying to bind a list of elements to my listview, each elements has some propery but i need to bind only following property: Type.

I have read this thread that is very clear but it's not working for me, my listview remains empty... i checked if the list that i bind to listview contains elements and it contains them.

Also i tried to bind a simple "List MyList" and without ListView.ItemTemplate and it works

Following an example of my code, i have this structures:

public class MainType
{
    public string Type { get; set;}
}

public class ChildType : MainType
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

public class SimpleClass
{
    public ObservableCollection<ChildType> SimpleList { get; set; } 

    public SimpleClass()
    {
        SimpleList  = new ObservableCollection<ChildType>();
        SimpleList.Add(new ChildType { Prop1 = "prop1", Prop2 = "prop2", Type = "type1" }
        SimpleList.Add(new ChildType { Prop1 = "prop1", Prop2 = "prop2", Type = "type2" }
    }
}

In the source where i have my list view i have this on xaml side:

           <ListView x:Name="myListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Type}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

and this on C# side:

public sealed partial class MyUserControl : UserControl
{ 
    private SimpleClass MySimpleClass;

    public MyUserControl()
    {
       this.InitializeComponent(); 
       myListView.DataContext = MySimpleClass = new SimpleClass();
       myListView.ItemsSource = MySimpleClass.SimpleList;
    }
}

Anyone can tell me what i missing?

Thanks!

[EDIT: The above code is correct, the problem was another. ]

The problem was another, inside SimpleClass through another method i deserialize a json file to SimpleList, and it create problems to binding because also if afterwards i simply add an element through ADD method, binding was not working... but if i deserialize the json file to another list: TempList and after through a foreach i add all elements of TempList to SimpleList, it works.

Binding doesn't work:

SimpleList = JsonConvert.DeserializeObject<ObservableCollection<ChildType>>(text, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });

Binding works:

TempList = JsonConvert.DeserializeObject<ObservableCollection<ChildType>>(text, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
foreach(ChildType item in TempList)
{
   SimpleList.Add(item);
} 
Community
  • 1
  • 1
Dendi
  • 1
  • 2
  • I read the below comments. You have to Implement INotifyPropertyChanged in SimpleClass to reflect the items added to the list to UI – Archana May 10 '16 at 08:49
  • Hi LovetoCode, thank you for your reply, i know that INotifyPropertyChanged is used to update the list when the property changes, but in my case i add a new element, for this i haven't used INotifyPropertyChanged but i have used ObservableCollection<> instead a simple List<> , i will try your tip and i will let you know, thanks! – Dendi May 10 '16 at 08:58
  • No INotifyPropertyChanged doesn't helped but i found the problem.. In my code i add new elements to SimpleList from a json file: SimpleList = JsonConvert.DeserializeObject>(text, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); Like this is not working and if i add another element from code trough ADD method, also this element is not showed in listview. But if i create another list: TempList, to which i deserialize my json file, and after, trough a foreach from TempList i add all elements to my SimpleList, it works! :) – Dendi May 10 '16 at 09:28

1 Answers1

0

You could tried your source as an XML and use XmlSerializer to map the Class object to the source

Call

  var objectType = typeof(DataClass.AssessmentLayoutDataClass);
                    return (DataClass.AssessmentLayoutDataClass)Utilities.XmlFunctions.ObjectFromXMLString(objectType, found.DataElements);

Method:

  public static object ObjectFromXMLString(Type typeOfObject, string xmlFile)
    {
        XmlSerializer serializer = new XmlSerializer(typeOfObject);
        StringReader rdr = new StringReader(xmlFile);
        return serializer.Deserialize(rdr);
    }
Chris Cooper
  • 389
  • 3
  • 16
  • Hi Chris! Thank you for your reply, after finishing to write my thread i notice that the problem was another... If i add elements to the list in the constructor method like in my example, it works, the problem is when i add elements in run code... like when i push a button in my list i add an element but this element is not showed in the list view... I was deleting this thread but i seen your reply and i thought it was correctly to reply you before – Dendi May 10 '16 at 08:39
  • I find, once you put your question up you find the answer or your find it was not the issue. Have you resolve the issue tho? – Chris Cooper May 10 '16 at 08:44
  • Would this not resolve the issue: myListView.Items.Add(new ChildType(){ Prop1 = " ", Prop2 = "", Type = ""}); – Chris Cooper May 10 '16 at 08:48
  • Yes my find was not the issue... sorry, i must to delete the thread? – Dendi May 10 '16 at 08:49