例子
1、布局
<Grid x:Name="rootLayout" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1">
<TextBlock Text="{Binding Path=[3].Name }" Style="{StaticResource BasicTextStyle}" Margin="5"/>
<TextBlock Text="{Binding Path=[3][Gaffer]}" Style="{StaticResource BasicTextStyle}" Margin="5"/>
</Grid>
2、填充DataContext
rootLayout.DataContext = new Teams();
3、辅助类
public class Team //Has a custom string indexer
{
Dictionary<string, object> _propBag;
public Team()
{
_propBag = new Dictionary<string, object>();
}
public string Name { get; set; }
public string City { get; set; }
public SolidColorBrush Color { get; set; }
// 对_propBag进行索引
public object this[string indexer]
{
get
{
return _propBag[indexer];
}
set
{
_propBag[indexer] = value;
}
}
public void Insert(string key, object value)
{
_propBag.Add(key, value);
}
}
// This class is used to demonstrate grouping.
public class Teams : List<Team>
{
public Teams()
{
Add(new Team() { Name = "The Reds", City = "Liverpool", Color = new SolidColorBrush(Colors.Green) });// 0
Add(new Team() { Name = "The Red Devils", City = "Manchester", Color = new SolidColorBrush(Colors.Yellow) });// 1
Add(new Team() { Name = "The Blues", City = "London", Color = new SolidColorBrush(Colors.Orange) });// 2
Team _team = new Team() { Name = "The Gunners", City = "London", Color = new SolidColorBrush(Colors.Red) };
_team["Gaffer"] = "le Professeur";
_team["Skipper"] = "Mr Gooner";
Add(_team);// 3
}
}