win8第八步ListView、ComboBox、FlipView的使用
ObservableCollection动态数据集,集合中的发生改变这3个控件会在控件上体现出来,以后尽量使用ObservableCollection绑定控件,而不是用List
也可以自己创建动态数据集只要类继承INotifyCollectionChanged
它与INotifyPropertyChanged一个是控制实体类的一个是控制集合的
ListView
IsItemClickEnabled开启Item点击事件
SelectionMode选中模式None不可以选中、Single单选、Multiple多选
<ListView.ItemTemplate>设置ListView中显示样式,下为一个TextBox和TextBlock
lv1.ItemsSource = list;绑定数据
如果你把ItemsSource="{Binding}"用 lv1.DataContext = list;属性也可以
<ListView IsItemClickEnabled="True" SelectionMode="Multiple" Name="lv1" HorizontalAlignment="Left" Height="608" Margin="225,94,0,0" VerticalAlignment="Top" Width="663" Grid.ColumnSpan="2" ItemClick="lv1_ItemClick"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBox Text="{Binding Name}"></TextBox> <TextBlock Text="{Binding Age}"></TextBlock> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>
获取ListView选中项lv1.SelectedItem单选选中,lv1.SelectedItems多选选中
ListView的itemclick事件
private void lv1_ItemClick(object sender, ItemClickEventArgs e) { //首先设置IsItemClickEnabled="True"启动ItemClick事件 //监听ItemClick事件,e.ClickedItem为点击的那一项DataContext Person p = e.ClickedItem as Person; MessageDialog mdl = new MessageDialog(p.ToString()); mdl.ShowAsync(); }
ComboBox
<ComboBox.ItemTemplate>设置中显示样式,下为一个Image和TextBlock
cbCon.ItemsSource = list;绑定
<ComboBox Name="cbCon" HorizontalAlignment="Left" Margin="151,120,0,0" VerticalAlignment="Top" Width="454"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding ImagePath}" Height="50" Width="50" /> <TextBlock Text="{Binding Name}" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
FlipView
可实现左右滑屏
<FlipView.ItemTemplate>设置中显示样式,下为一个两行一列的grid中也有2个Image,第一个Image跨两行
<FlipView Name="flipview1" HorizontalAlignment="Left" Margin="688,55,0,0" VerticalAlignment="Top" Width="552" Height="236"> <FlipView.ItemTemplate> <DataTemplate> <Grid Background="Blue"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Image Source="{Binding ImagePath}" Grid.RowSpan="2"></Image> <Image Source="ms-appx:///Image/un.jpg" Height="50" Width="50" Visibility="{Binding IsUn,Converter={StaticResource boolvisibilityconverter}}"></Image> </Grid> </DataTemplate> </FlipView.ItemTemplate> </FlipView>
Visibility="{Binding IsUn,Converter={StaticResource boolvisibilityconverter}}"为显示一种转换bool转换为Visibility
继承IValueConverter接口
public class BoolVisibilityConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { bool b = (bool)value; return b ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, string language) { Visibility v = (Visibility)value; return v == Visibility.Visible; } }
<Page x:Class="App4.BlankPage2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App4" xmlns:common="using:App4" 类的命名空间设置的名称为common xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Page.Resources> 设置资源common:BoolVisibilityConverter然后就可以使用了
<common:BoolVisibilityConverter x:Key="boolvisibilityconverter"> </common:BoolVisibilityConverter> </Page.Resources>