I need to do a ListView that has a single column, where this colum is an Expander.
In particular I have a class like this:
public class MyItemT
{
public string Title { get; set; }
public TextBlock Info { get; set; }
}
And a list of these objects:
List<MyItemT> Mylist = new List<MyItemT>();
I need to fill the ListView with Expander objects, where the Header it's binded with Title and the Content with the Info property.
For example a ListView of Expander like this:
<Expander Header="My Expander">
<TextBlock TextWrapping="Wrap">
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua
</TextBlock>
</Expander>
... and so on (many as there are elements of my ListView) ...
The ListView should not have the header bar, and of course everything would be done programmatically.
How it should be done? Thanks.
Just define an ItemTemplate for your ListView and place an Expander inside. This at least works for other controls inside an ListView, so it should work here as well.
<ListView ItemsSource="{Binding YourSource}" Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Title}">
<TextBlock TextWrapping="Wrap" Text="{Binding Info}" />
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The binding can also be done via code like this.
List<MyItemT> Mylist = new List<MyItemT>();
listView.ItemsSource = Mylist;
One thing you gotta change is the type of your Info property, it should be of type string.
Oh, and your class should implement INotifyPropertyChanged
, otherwise the binding will not always work correctly.