listbox横向排列

在Listbox中横向显示CheckBox

前台代码

1 <ListBox Height="30" HorizontalAlignment="Left" Margin="231,25,0,0" Name="listMonth" VerticalAlignment="Top" Width="747" >
2             <StackPanel x:Name="sp" Orientation="Horizontal">
3                 需要添加的元素
4             </StackPanel>
5 </ListBox>

后台动态添加CheckBox

1 foreach(var i in listOfMonth)
2             {
3                 CheckBox cbMonth = new CheckBox();
4                 cbMonth.Content = new TextBlock() { Text = i + "月?" };
5                 cbMonth.IsChecked = true;
6                 cbMonth.Checked += new RoutedEventHandler(cbMonth_Checked);
7                 cbMonth.Unchecked += new RoutedEventHandler(cbMonth_Checked);
8                 this.sp.Children.Add(cbMonth);    
9             }
View Code

循环读取

1 foreach (var item in sp.Children)
2                 {
3                     if ((item as CheckBox).IsChecked ?? false)
4                     {
5                         int Month = Convert.ToInt32(((item as CheckBox).Content as TextBlock).Text.ToCharArray()[0].ToString());
6                      }
7                 }
View Code


除了这种方法外还有一种方式:横排模版

 1 <ListBox ItemsSource="{Binding DishesList}" Margin="0,58,0,21"
 2                      ScrollViewer.VerticalScrollBarVisibility="Disabled" 
 3 ScrollViewer.HorizontalScrollBarVisibility="Disabled">
 4                 <ListBox.ItemsPanel>
 5                     <ItemsPanelTemplate>
 6                         <toolkit:WrapPanel Orientation="Horizontal"/>
 7                     </ItemsPanelTemplate>
 8                 </ListBox.ItemsPanel>
 9                 <ListBox.ItemTemplate >                  
10                     <DataTemplate>                        
11                         <!--修改圆角外边-->
12                     <Border BorderThickness="5"  BorderBrush="#FFD9D6D6" Margin="0,5,0,0" CornerRadius="5" >
13                         <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"  Width="300"
14                                     Background="#FFD9D6D6">
15                             <StackPanel Height="80" Margin="10,0,0,0" HorizontalAlignment="Center"  Background="#FFF5F3F3">
16                                     <Image
17                                         Source="{Binding dishesimage, Converter={StaticResource ImageConver}, Mode=TwoWay}"
18                                         Style="{StaticResource imagestyle}" />
19                                 <Canvas  Height="1" Margin="0,40,0,0" Background="Black"/>
20                             </StackPanel>
21                             <StackPanel Width="250" Margin="20,0,0,0">
22                                     <TextBlock  Text="{Binding dishesname}" Style="{StaticResource titlestyle}"/>
23                                     <TextBlock  Text="{Binding dishesprice,Converter={StaticResource DispicConver},Mode=TwoWay}" Style="{StaticResource authorstyle}"/>
24                                     <TextBlock  Text="{Binding typename}" Style="{StaticResource timestyle}"/>
25                                 <!--<Canvas  Height="1" Margin="0,40,0,0" Background="Black"/>-->
26                             </StackPanel>
27                         </StackPanel>
28                     </Border>
29                 </DataTemplate>
30             </ListBox.ItemTemplate>
31             </ListBox>
View Code

 

posted @ 2015-01-31 14:24  童心少年  阅读(563)  评论(0编辑  收藏  举报