这两个家伙,对我们来说,是绝对不陌生的,从WinForm到WPF,到Asp.net,我们都会接触到这两个控件,而且我相信我们也经常使用。
ListBox
先说ListBox,这个其实很简单,应该说,对于所有的集合控件,都是一样的使用方法,往里面放东西就两种途径:
1、数据绑定;
2、手动添加项。
而ListBox对应的项是ListBoxItem,说得更明白一些,它就是一个ContentControl,就像Button一样,都有一个Content属性,而我们就通过这个属性来设置项里面要显示的东西。对于数据绑定而言,因为是批量化的,所以,这个时候,我们就要用上DataTemplate了。
先来看看ListBox的数据绑定的例子。
- <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
- <ListBox x:Name="list" Width="365"/>
- </Grid>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ListBox x:Name="list" Width="365"/>
</Grid>
在代码视图中,我们在MainPage类的构造函数中设置数据源,其实就是设置它的ItemsSource属性。
- public MainPage()
- {
- this.InitializeComponent();
- string[] items = { "夏", "商", "周", "春秋战国", "秦", "汉", "三国魏晋南北朝", "隋", "唐" };
- this.list.ItemsSource = items;
- }
public MainPage()
{
this.InitializeComponent();
string[] items = { "夏", "商", "周", "春秋战国", "秦", "汉", "三国魏晋南北朝", "隋", "唐" };
this.list.ItemsSource = items;
}
然后按下F5,你能看到以下的效果。
可是,有时候,似乎只显示一个文本还不够,有可能我们用的数据源较为复杂。
好的,现在我们来伪造一些测试数据,先定义实体类。
- public class Employee
- {
- public string EmName { get; set; }
- public string EmNo { get; set; }
- public int EmAge { get; set; }
- public Windows.UI.Xaml.Media.Imaging.BitmapImage Photo { get; set; }
- }
public class Employee
{
public string EmName { get; set; }
public string EmNo { get; set; }
public int EmAge { get; set; }
public Windows.UI.Xaml.Media.Imaging.BitmapImage Photo { get; set; }
}
接着弄好XAML。
- <ListBox x:Name="listEmp" SelectionMode="Single">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto"/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Grid Margin="10" Grid.Column="1">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="auto"/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <TextBlock Grid.Row="0" Grid.ColumnSpan="2" FontWeight="Bold" FontSize="24" Margin="3,2,3,2" Text="{Binding EmName}"/>
- <TextBlock Text="工号:" Grid.Row="1" Grid.Column="0"/>
- <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding EmNo}"/>
- <TextBlock Grid.Row="2" Grid.Column="0" Text="年龄:"/>
- <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding EmAge}"/>
- </Grid>
- <Image Grid.Column="0" Margin="4" Source="{Binding Photo}" Stretch="Uniform"/>
- </Grid>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
<ListBox x:Name="listEmp" SelectionMode="Single">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Margin="10" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" FontWeight="Bold" FontSize="24" Margin="3,2,3,2" Text="{Binding EmName}"/>
<TextBlock Text="工号:" Grid.Row="1" Grid.Column="0"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding EmNo}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="年龄:"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding EmAge}"/>
</Grid>
<Image Grid.Column="0" Margin="4" Source="{Binding Photo}" Stretch="Uniform"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在代码视图中进行绑定。
- List<Employee> empList = new List<Employee>();
- Employee e1 = new Employee
- {
- EmName = "胡扯",
- EmNo = "HC-22556854",
- EmAge = 38,
- Photo = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/1.jpg"))
- };
- empList.Add(e1);
- Employee e2 = new Employee
- {
- EmName = "张大腿",
- EmNo = "HC-62254585",
- EmAge = 41,
- Photo = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/1.jpg"))
- };
- empList.Add(e2);
- Employee e3 = new Employee
- {
- EmName = "草先生",
- EmNo = "HC-2000355462",
- EmAge = 41,
- Photo = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/1.jpg"))
- };
- empList.Add(e3);
- this.listEmp.ItemsSource = empList;
List<Employee> empList = new List<Employee>();
Employee e1 = new Employee
{
EmName = "胡扯",
EmNo = "HC-22556854",
EmAge = 38,
Photo = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/1.jpg"))
};
empList.Add(e1);
Employee e2 = new Employee
{
EmName = "张大腿",
EmNo = "HC-62254585",
EmAge = 41,
Photo = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/1.jpg"))
};
empList.Add(e2);
Employee e3 = new Employee
{
EmName = "草先生",
EmNo = "HC-2000355462",
EmAge = 41,
Photo = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/1.jpg"))
};
empList.Add(e3);
this.listEmp.ItemsSource = empList;
这里我们就用到了数据模板了,因为我们每一项要显示的内容有员工头像,姓名,工号,年龄多个字段,明显仅依靠一个字符串是做不到的,因此,必要时,我们出动数据模板。
可能有人会想了,这ListBox默认是纵向排列的,有没有办法让它的项水平排列呢?当然有,ItemsPanel属性正是让我们来决定它的项列表如何排列的,它可以设置一个面板,如
- <ListBox.ItemsPanel>
- <ItemsPanelTemplate>
- <StackPanel Orientation="Horizontal"/>
- </ItemsPanelTemplate>
- </ListBox.ItemsPanel>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
再运行一下,你就能看到它们水平排列了。
怎么样,这三位哥们儿挺帅的吧。
接下来是手动添加项,这个就更简单了,看看下面的XAML你就懂了。
- <ListBox>
- <ListBoxItem>项目一</ListBoxItem>
- <ListBoxItem>
- <ListBoxItem.Content>
- <Rectangle Width="480" Height="80" Fill="Orange"/>
- </ListBoxItem.Content>
- </ListBoxItem>
- <ListBoxItem>
- <ListBoxItem.Content>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="auto"/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <TextBlock Grid.Column="0" Text="请输入一个数字:" FontSize="25"/>
- <TextBox Grid.Column="1" Width="260"/>
- </Grid>
- </ListBoxItem.Content>
- </ListBoxItem>
- </ListBox>
<ListBox>
<ListBoxItem>项目一</ListBoxItem>
<ListBoxItem>
<ListBoxItem.Content>
<Rectangle Width="480" Height="80" Fill="Orange"/>
</ListBoxItem.Content>
</ListBoxItem>
<ListBoxItem>
<ListBoxItem.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="请输入一个数字:" FontSize="25"/>
<TextBox Grid.Column="1" Width="260"/>
</Grid>
</ListBoxItem.Content>
</ListBoxItem>
</ListBox>
运行后就是这样:
ComboBox
其实与ListBox是一样的,只不过它是一个下拉列表框罢了。其项列表对应着是ComboBoxItem类。
考虑下面的例子,我们在ComboBox里面放些东东,然后,当我们选择了特定项后,TextBlock中文本的颜色随之改变。
1、先布局UI。
- <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
- <StackPanel>
- <ComboBox x:Name="cb" Width="275" HorizontalAlignment="Left">
- <ComboBox.ItemTemplate>
- <DataTemplate>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="auto"/>
- <ColumnDefinition Width="*"/>
- </Grid.ColumnDefinitions>
- <Rectangle Grid.Column="0" Width="25" Height="25" Margin="5" Fill="{Binding Path=Brush}"/>
- <TextBlock Grid.Column="1" Text="{Binding Path=ColorName}" VerticalAlignment="Center" Margin="3,2,2,2"/>
- </Grid>
- </DataTemplate>
- </ComboBox.ItemTemplate>
- </ComboBox>
- <TextBlock Margin="5,20,0,0" FontSize="60" FontWeight="Black" Text="文本颜色" x:Name="tb"/>
- </StackPanel>
- </Grid>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<ComboBox x:Name="cb" Width="275" HorizontalAlignment="Left">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Width="25" Height="25" Margin="5" Fill="{Binding Path=Brush}"/>
<TextBlock Grid.Column="1" Text="{Binding Path=ColorName}" VerticalAlignment="Center" Margin="3,2,2,2"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Margin="5,20,0,0" FontSize="60" FontWeight="Black" Text="文本颜色" x:Name="tb"/>
</StackPanel>
</Grid>
接下来嘛,你猜到了,肯定是写处理代码了。
在MainPage类的构造函数中,我们为ComboBox添加4个可选项。并且处理它的SelectionChanged事件,对于绑定,我们这次不必定义一个类这么麻烦,别忘了,.net里面有一个很灵活的类型——dynamic,再配全new 关键字,我们就可以动态定义一个对象出来了。
- public MainPage()
- {
- this.InitializeComponent();
- List<dynamic> colorList = new List<dynamic>();
- colorList.Add(new {
- Brush = new SolidColorBrush(Colors.Yellow),
- ColorName = "黄色"
- });
- colorList.Add(new
- {
- Brush = new SolidColorBrush(Colors.Blue),
- ColorName = "蓝色"
- });
- colorList.Add(new
- {
- Brush = new SolidColorBrush(Colors.Gray),
- ColorName = "灰色"
- });
- colorList.Add(new
- {
- Brush = new SolidColorBrush(Colors.Pink),
- ColorName = "粉红色"
- });
- this.cb.ItemsSource = colorList;
- // 绑定事件,对ComboBox的选择作出响应
- cb.SelectionChanged += (a, args) =>
- {
- if (args.AddedItems.Count <= 0)
- {
- return;
- }
- dynamic item = (dynamic)args.AddedItems[0];
- if (item != null)
- {
- this.tb.Foreground = item.Brush;
- }
- };
- if (cb.Items.Count > 0)
- {
- cb.SelectedIndex = 0;
- }
- }
public MainPage()
{
this.InitializeComponent();
List<dynamic> colorList = new List<dynamic>();
colorList.Add(new {
Brush = new SolidColorBrush(Colors.Yellow),
ColorName = "黄色"
});
colorList.Add(new
{
Brush = new SolidColorBrush(Colors.Blue),
ColorName = "蓝色"
});
colorList.Add(new
{
Brush = new SolidColorBrush(Colors.Gray),
ColorName = "灰色"
});
colorList.Add(new
{
Brush = new SolidColorBrush(Colors.Pink),
ColorName = "粉红色"
});
this.cb.ItemsSource = colorList;
// 绑定事件,对ComboBox的选择作出响应
cb.SelectionChanged += (a, args) =>
{
if (args.AddedItems.Count <= 0)
{
return;
}
dynamic item = (dynamic)args.AddedItems[0];
if (item != null)
{
this.tb.Foreground = item.Brush;
}
};
if (cb.Items.Count > 0)
{
cb.SelectedIndex = 0;
}
}