Problem in dynamic WPF ListView binding

A

avital

Hello all,
I have this problem regarding binding WPF ListView:
I have a List of a custom class, this list is bind to a ListView which
is also defined.
I would like to load the data from the list into the ListView
dynamically.

I have a XAML file which contains the definition of the ListView.
Then in code, I define the columns of the ListView according to the
Properties of my custom class, everything is OK until now.

Then I bind the ListView object to my list.

Then when the ListView is displayed, all rows seem to have the same
value. Actually the first row is duplicated as the number of actuall
rows.

But, when I click on one of the rows in the Listview and I see the
content of the selected row, I see that its fields contain the correct
value and not the value which is displayed (the wrong one).

Does anyone have a suggestion of how to solve this problem?

I add the code snippet of the XAML file and the internal code.

Thanks,
Avital.


XAML:
------------
<Window x:Class="listview_example.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="466"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Canvas Height="271" Name="canvas1" Width="444">
<ListView Canvas.Left="0" Canvas.Top="0" Height="271"
Name="listView1"
SelectionChanged="listView1_SelectionChanged"/>
</Canvas>
</Window>


CODE:
-----------------


namespace listview_example
{

public class Person
{

[SortOrder(0)]
public String LastName { get; set; }


public Person(String lastName)
{
// FirstName = lastName;
LastName = lastName;
}

}


/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{

List<Person> _lst = new List<Person>();

public Window1()
{
InitializeComponent();

Person person = new Person("vvv");
_lst.Add(person);

Person aa = new Person("aa");
_lst.Add(aa);

CreateGenericGridView(_lst, person);

}

private Binding CreateBinding(object o, string path)
{
Binding b = new Binding(path);
b.Source = o;
return b;
}


private void AddGridViewColumn(
GridView gv,
object objToShow,
String property)
{
GridViewColumn gvc = new GridViewColumn();
gvc.Header = property;
gvc.DisplayMemberBinding = CreateBinding(objToShow,
property);
gv.Columns.Add(gvc);
}


private void CreateGenericGridView(List<Person> obj, object
single)
{
PropertyInfo[] properties = single.GetType().GetProperties
();

SortedDictionary<Int32, String> propertyCol =new
SortedDictionary<int, string>();
foreach (PropertyInfo pi in properties)
{
object[] customAtts = pi.GetCustomAttributes(true);
foreach (object o in customAtts)
{
SortOrderAttribute soa = o as SortOrderAttribute;
if (soa != null)
{
propertyCol.Add(soa.SortOrder, pi.Name);
break;
}
}
}


// create GridView columns
GridView gv = new GridView();
if (propertyCol.Count > 0)
{
SortedDictionary<Int32, String>.Enumerator enumerator
=
propertyCol.GetEnumerator();
while (enumerator.MoveNext())
{
AddGridViewColumn(gv, obj,
enumerator.Current.Value);
}
}

// associates the gridview with the listview
listView1.View = gv;

// Set binding for the listview
Binding binding = new Binding();
binding.Source = _lst;
listView1.SetBinding(ListView.ItemsSourceProperty,
binding);


}

public List<Person> RowsCollection
{
get { return _lst; }
}

private void listView1_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
ListView ll = sender as ListView;
IList selectedItems = e.AddedItems;
IList unselected = e.RemovedItems;
}

}


public class SortOrderAttribute : Attribute
{
public Int32 SortOrder { get; set; }
public SortOrderAttribute(Int32 sortOrder)
{
SortOrder = sortOrder;
}
}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top