作为一个WPF初学者,感到困难重重。在网上想查个ListBox绑定数据源的示例,结果出来一大堆代码,一看心就烦。
我给个简洁一点的代码:
后台代码:
protected class UserItem { public UserItem(int Id, string Name, bool IsActived) { this.Id = Id; this.Name = Name; this.IsActived = IsActived; } public int Id{get;set;} public string Name { get; set; } public bool IsActived { get; set; } public string BackGround { get { return IsActived ? "/test;component/Assets/Images/UserItemNull.png" : "/test;component/Assets/Images/UserItemNullg.png"; } } } void Init() { Lst.ItemsSource = new List<UserItem> { new UserItem(1,"张三",true) ,new UserItem(2,"李四",true) ,new UserItem(3,"赵五",true) ,new UserItem(4,"钱六",true) ,new UserItem(5,"孙七",false) ,new UserItem(6,"李八",false) ,new UserItem(7,"王九",false) ,new UserItem(8,"陈十",false) ,new UserItem(9,"吴万",false) ,new UserItem(10,"刘十八",false) }; }
页面代码:
<Grid> <ListBox x:Name="Lst"> <ListBox.ItemTemplate> <DataTemplate> <Button MouseDoubleClick="Button_MouseDoubleClick"> <Grid> <Image Source="{Binding Path=BackGround}" /> <TextBlock Text="{Binding Path=Name}" Margin="70 10" FontSize="18"></TextBlock> </Grid> </Button> </DataTemplate> </ListBox.ItemTemplate> </Grid>
但我们就是要被迫这样子不停地学习、学习,直到升为管理层,或被淘汰。