SilverLight数据绑定泛型List
昨天晚上搞了一晚上,就为了一个泛型数据绑定
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 c#
2
3 void BlogPage_Loaded(object sender, RoutedEventArgs e)
4 {
5 List<test> items = new List<test>();
6 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
7 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
8 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
9 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
10 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
11 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
12 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
13 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
14 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
15
16 BlogTypeList.DataContext = items;
17 }
18
19
20
21 public class test
22 {
23 public string BlogType;
24 }
25 -------------------------------------------------------------------------------------
26 xaml
27
28 <ListBox x:Name="BlogTypeList" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding}" Style="{StaticResource BlogTypeListBoxStyle}" />
29
30 <!--ListBox Style-->
31 <Style x:Key="BlogTypeListBoxStyle" TargetType="ListBox">
32 <Setter Property="Background" Value="Transparent"/>
33 <Setter Property="Padding" Value="2,2,auto,2"/>
34 <Setter Property="BorderThickness" Value="0"/>
35 <Setter Property="MaxWidth" Value="200"/>
36 <Setter Property="ItemTemplate">
37 <Setter.Value>
38 <DataTemplate>
39 <TextBlock Text="{Binding BlogType}" Foreground="White"/>
40 </DataTemplate>
41 </Setter.Value>
42 </Setter>
43 </Style>
44
2
3 void BlogPage_Loaded(object sender, RoutedEventArgs e)
4 {
5 List<test> items = new List<test>();
6 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
7 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
8 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
9 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
10 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
11 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
12 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
13 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
14 items.Add(new test { BlogType = "xxdfffffffffffffffffwefwefwf" });
15
16 BlogTypeList.DataContext = items;
17 }
18
19
20
21 public class test
22 {
23 public string BlogType;
24 }
25 -------------------------------------------------------------------------------------
26 xaml
27
28 <ListBox x:Name="BlogTypeList" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding}" Style="{StaticResource BlogTypeListBoxStyle}" />
29
30 <!--ListBox Style-->
31 <Style x:Key="BlogTypeListBoxStyle" TargetType="ListBox">
32 <Setter Property="Background" Value="Transparent"/>
33 <Setter Property="Padding" Value="2,2,auto,2"/>
34 <Setter Property="BorderThickness" Value="0"/>
35 <Setter Property="MaxWidth" Value="200"/>
36 <Setter Property="ItemTemplate">
37 <Setter.Value>
38 <DataTemplate>
39 <TextBlock Text="{Binding BlogType}" Foreground="White"/>
40 </DataTemplate>
41 </Setter.Value>
42 </Setter>
43 </Style>
44
老是绑定不成功,早上起来,又继续搞,最后发现问题所在:
是实体类test搞得鬼,正确的实体类应该这样
public class test
{
public string BlogType{get;set;}
}
{
public string BlogType{get;set;}
}
需要用属性。
由此可见,SilverLight内部绑定中需要用到反射来获取类的属性。