Hi,
I have a collection (called People) that I want display on a list box. The People is a collection of person with each row has a Site instance. The output I want to see is
Johnny A Apple
Maria B Banana
Slick C Custard Apple
but kept getting
Johnny UserControlXAML.Site
Maria UserControlXAML.Site
Slick UserControlXAML.Site
I have defined two typed data templates one for Person and Site. But it does not seem to recognize the Site. Person has an instance of a Site. Thanks in advance.
Regards,
PS: Here is my code
<
Window x:Class="UserControlXAML.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:UserControlXAML" Title="Example" Height="500" Width="500">
<
Window.Resources><!--
This template explains how to display a Person object. --><
DataTemplate DataType="{x:Type local:Site}"><
StackPanel Margin="2,4" Orientation="Horizontal"><
TextBlock Text="{Binding Path=Code}" /><
TextBlock Text="{Binding Path=Description}" /></
StackPanel></
DataTemplate><
DataTemplate DataType="{x:Type local:Person}"><
StackPanel Margin="2,4" Orientation="Horizontal"><
TextBlock Text="{Binding Path=Name}" /><
TextBlock Text=" " /><
TextBlock Text="{Binding Path=MySite}"/></
StackPanel></
DataTemplate><!--
Our dummy data source with three people in it. --><
local:PersonCollection x:Key="people" /></
Window.Resources><
Grid DataContext="{StaticResource people}"><
Grid.RowDefinitions><
RowDefinition Height="Auto" /></
Grid.RowDefinitions><
ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" /></
Grid></
Window>namespace
UserControlXAML{
public class Site{
private int _id; private string _code; private string _description; public Site(int id, string code, string description){
_id = id;
_code = code;
_description = description;
}
public int ID{
get{
return _id;}
set{
_id =
value;}
}
public string Code{
get{
return _code;}
set{
_code =
value;}
}
public string Description{
get{
return _description;}
set{
_description =
value;}
}
}
public class Person{
private string _name; private Site _mysite; public Person(string name, Site mysite){
_name = name;
_mysite = mysite;
}
public string Name{
get{
return _name;}
}
public Site MySite{
get{
return _mysite;}
}
}
class PersonCollection : List<Person>{
public PersonCollection(){
base.Add(new Person("Johnny", new Site(1, "A", "Apple"))); base.Add(new Person("Maria", new Site(2, "B", "Banana"))); base.Add(new Person("Slick", new Site(3, "C", "Custard Apple")));}
}
}

Typed data template
vic001
Add a x:Key attribute('dtSite') to the datatemplate and change the following
<GridViewColumn Header="Site" DisplayMemberBinding="{Binding MySite}" />
to
<GridViewColumn Header="Site" CellTemplate="{StaticResource dtSite}" />
KrazyMGA
Try replacing
<TextBlock Text="{Binding Path=MySite}"/>
with:
<ContentControl Content="{Binding Path=MySite}" />
The Text property of TextBlock is of type string, which prevents the use of your DataTemplate.
jeff w smith
jakeb16
Thanks for that worked - but how can it use it with GridViewColumn
<
ListView ItemsSource="{Binding Source={StaticResource people}}"><
ListView.View><
GridView><
GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Name}" /><
GridViewColumn Header="Site" DisplayMemberBinding="{Binding MySite}" /></
GridView></
ListView.View></
ListView>I get the same problem. Thanks.