WP7中类似系统颜色选择的控件PickerBox的使用
2012-04-20 00:54
387 查看
WP7中,有时候UI有这样的需求,需要给用户一个选择项,但是这个选择项的数目很多.这时候下拉选择就不适合了.在系统中有一个很好的实现方式,就是在系统主题设置颜色的方式.这种方式可以很好的解决这个问题.不过可惜的是,不管是在SDK中还是在TOOLKIT中,都没有类似的控件.不过好在,有人已经写好了.我们直接拿来用,就不用重复造轮子了.
控件效果是这样的.
既然控件已经有人写好了,那么我们来看看怎么调用.
先看简单的,只有文字的.
在强大的封装下,一切都很简单.
很明显,有时候单纯的一行文字并不能满足要求.接下来我们来看复杂的,如何自定义他.
首先,要定义一个Style
这个Style就是Dialog的样式,
然后和之前一样,绑定数据源,然后把Dialog显示出来
ViewModule 和 Converter的代码就不贴出来了,和一般绑定一个LISTVIEW的数据写法是一样的.
完整代码和控件到附件中去下载.
来自WPDevN: http://www.wpdevn.com/showtopic-109.aspx
控件效果是这样的.
既然控件已经有人写好了,那么我们来看看怎么调用.
先看简单的,只有文字的.
private string[] data = new string[] { "erte 1", "ywfd 2", "ksdd 3", "jmas 4", "laer 5", "oper 6", "hola 7", "mora 8", "hdrf 9", "vdfe 10", "gfds 11" }; private void InitPickerBoxDialog() { PickerBoxDialog dialog = new PickerBoxDialog(); // Item的内容 dialog.ItemSource = data; //标题 dialog.Title = "FIRST DAY OF THE WEEK"; // Dialog关闭的时候触发的事件 dialog.Closed += new EventHandler(dialog_Closed); dialog.show(); } void dialog_Closed(object sender, EventArgs e) { string selcontent = data[selectedIndex]; }
在强大的封装下,一切都很简单.
很明显,有时候单纯的一行文字并不能满足要求.接下来我们来看复杂的,如何自定义他.
首先,要定义一个Style
<Style TargetType="control:PickerBoxDialog" x:Key="Custom"> <Setter Property="Background" Value="{StaticResource PhoneChromeBrush}"/> <Setter Property="Width" Value="480" /> <Setter Property="Height" Value="800" /> <Setter Property="Margin" Value="0" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="control:PickerBoxDialog"> <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" > <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,30,0,40"> <TextBlock x:Name="DialogTitle" Text="MY DIALOG TITLE" Style="{StaticResource PhoneTextNormalStyle}" /> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" > <ListBox Name="listBox" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel x:Name="item" Orientation="Horizontal" Margin="5, 25, 0, 25"> <Rectangle Fill="{Binding Color, Converter={StaticResource ColorToBrushConverter}}" Width="42" Height="42" /> <TextBlock Margin="15, 0, 0, 0" Text="{Binding Text}" FontSize="40" TextWrapping="Wrap" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
这个Style就是Dialog的样式,
然后和之前一样,绑定数据源,然后把Dialog显示出来
this.customDialog = new PickerBoxDialog(); this.customDialog.Title = "ACCENTS"; // Assign our style to the dialog this.customDialog.Style = this.Resources["Custom"] as Style; this.customDialog.ItemSource = viewModel.Items; this.customDialog.Closed += new EventHandler(customDialog_Closed); this.custionDialog.show();
ViewModule 和 Converter的代码就不贴出来了,和一般绑定一个LISTVIEW的数据写法是一样的.
完整代码和控件到附件中去下载.
来自WPDevN: http://www.wpdevn.com/showtopic-109.aspx
相关文章推荐
- Android 使用动画效果后的控件位置处理 类似系统通知栏下拉动画!!
- Android 使用动画效果后的控件位置处理 类似系统通知栏下拉动画
- 一个类似CColorDialog中颜色选择的控件实现
- WPF如何更改系统控件的默认高亮颜色 (Highlight brush)
- WP7弹出控件的使用
- Win10下python3和python2同时安装并解决pip共存问题 特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似。 使用python开
- Android中选择控件、自动填充、GridView控件的使用
- 使用 jQuery 属性选择器获取服务器控件 ClientID
- 如何在VB 中直接打开并选择目录,而不使用麻烦的多个控件?
- 安卓开发自带控件修改主颜色(以日期选择器DatePickerDialog为例)
- 【WP7进阶】——详细教你使用帮你赚钱的广告控件
- Yii 日历选择的使用。 CJuiDatePicker ,其它他集成了 jquery 自带的日历控件
- MongoDB安装与使用(基于Mac系统安装,其他系统类似)
- up和down还可以使用类似选择器的形式来捕获控件
- 牛腩购物网 8 整合用户留言 重装系统之后iis7.5 sql2008 vs2010安装顺序,AspNetPager 控件的使用,为什么 anp.StartRecordIndex 总是为1
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- WdatePicker日期选择控件使用时,Internet Explorer无法打开站点,已终止操作”问题的解决
- [Unity]Android(安卓)代码安卓APK时,使用系统安装器安装的[最正确]办法(不弹出选择打开的程序)
- 机房收费系统———DTpicker日期控件的使用
- 日历控件Calendar使用技巧--根据数据库数据控制日期背景显示颜色