Windows APP Binding之索引器

例子


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
        }
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值