为了演示效果,模拟了一个餐饮系统点餐的界面,不多说,直接上代码
前台
<ListBox x:Name="dglist" Grid.Row="1" Grid.Column="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" > <ListBox.ItemsPanel> <ItemsPanelTemplate> <toolkit:WrapPanel HorizontalAlignment="Left"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <Button Width="100" Height="80" Content="{Binding Name}" Background="{Binding State,Converter={StaticResource DeskStateConvert}}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
后台没什么代码,主要是给ListBox赋值
DeskCollection desks = new DeskCollection(); for (int i = 0; i < 21; i++) { int state = 0; if (i > 6) state = 1; if (i > 10) state = 2; if (i > 15) state = 3; Desk d = new Desk() { Name = "A" + i.ToString("00"), State = state }; desks.Add(d); } dglist.ItemsSource = desks;
效果预览