In this mini series "How to customize the WP7 ListBox Selected Item" of two posts I am going to demonstrate how to customize the ListBox Selected Item by modifying the ListBoxItem ControlTemplate as well as by changing the animations inside the Selected Visual State.
- How to customize the WP7 ListBox Selected Item | Part1: Control Template
- How to customize the WP7 ListBox Selected Item | Part2: Visual States
The first thing we need to consider is how our new Selected state will look like. We will add the following elements inside the ListBoxItem ControlTemplate:
NOTE: For more information about ControlTemplates in Windows Phone 7 take a look at our previous article: Working with ControlTemplates in Silverlight for WP7
Modifying the ListBoxItem ControlTemplate
We will use a RelativeSource binding so that we will be able to control the current Selected Item with the CheckBox. I.e. when an item is selected a checked CheckBox appears at a first place. After that if the user uncheck the CheckBox as a result the ListBox Item is deselected. (NOTE: It is important not to forget to set Mode=TwoWay):
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected, Mode=TwoWay}" ./>
The next important thing to do is to determine how to Show/Hide the CheckBox. Here we have several options:
Option1: You can control the Visibility with DoubleAnimation and change the Selected/UnSelected VisualStares. However this is not the best approach that we would suggest.
Option2: To bind the CheckBox Visibility property to the ListBoxItem IsSelectedProperty. This is definitely a better aproach but in this case we will need a BooleanToVisibilityConverter. You can either use some of the already implemented converters like those from the Coding4Fun toolkit or you can just grab the code:
1
2
3
4
5
6
7
8
9
10
11
12
|
public class BooleanToVisibilityConverter : IValueConverter { public object Convert( object value, Type targetType, object parameter, CultureInfo culture) { return System.Convert.ToBoolean(value) ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture) { return value.Equals(Visibility.Visible); } } |
Just paste this class somewhere in your WP7 app project and after that include it into the page resources:
1
2
3
4
5
|
< phone:PhoneApplicationPage ... xmlns:local = "clr-namespace:WP7ListBoxSelectedItemStyle" > < phone:PhoneApplicationPage.Resources > < local:BooleanToVisibilityConverter x:Key = "BooleanToVisibilityConverter" /> |
Here is how the code for the CheckBox inside ListBoxItem ControlTemplate should look like
1
2
3
4
|
< CheckBox x:Name = "checkBox" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path = IsSelected , Mode = TwoWay }" DataContext = "{TemplateBinding IsSelected}" Visibility = "{Binding Converter={StaticResource BooleanToVisibilityConverter}}" HorizontalContentAlignment = "{TemplateBinding HorizontalContentAlignment}" /> |
NOTE: You can easily get the default ListBox/ListBoxItem Style using Expression Blend. To do this just create a sample Windows Phone 7 Application in Expression Blend and then paste a ListBoxItem into the design surface.
After that just press right button and select EditTemplate - > Edit a Copy. By default Blend generate a Style called "ListBoxItemStyle1". So now you can grab the style and paste it into VisualStudio or modify it using Expression Blend. In our case we will paste "ListBoxItemStyle1" inside the page Resources. Note that the full Style is given below and also attached at the end of the article so that you can just copy and paste it without using Expression Blend!
Here is how the ListBox ControlTemplate should look like for now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< Style x:Key = "ListBoxItemStyle1" TargetType = "ListBoxItem" > ... < Setter Property = "Template" > < Setter.Value > < ControlTemplate TargetType = "ListBoxItem" > < Border x:Name = "LayoutRoot" BorderBrush = "{TemplateBinding BorderBrush}" BorderThickness = "{TemplateBinding BorderThickness}" Background = "{TemplateBinding Background}" HorizontalAlignment = "{TemplateBinding HorizontalAlignment}" VerticalAlignment = "{TemplateBinding VerticalAlignment}" > < VisualStateManager.VisualStateGroups > ... </ VisualStateManager.VisualStateGroups > < StackPanel x:Name = "border" Orientation = "Horizontal" > < CheckBox x:Name = "checkBox" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path = IsSelected , Mode = TwoWay }" DataContext = "{TemplateBinding IsSelected}" Visibility = "{Binding Converter={StaticResource BooleanToVisibilityConverter}}" HorizontalContentAlignment = "{TemplateBinding HorizontalContentAlignment}" /> < ContentControl x:Name = "ContentContainer" ContentTemplate = "{TemplateBinding ContentTemplate}" Content = "{TemplateBinding Content}" Foreground = "{TemplateBinding Foreground}" HorizontalContentAlignment = "{TemplateBinding HorizontalContentAlignment}" Margin = "{TemplateBinding Padding}" VerticalContentAlignment = "{TemplateBinding VerticalContentAlignment}" /> </ StackPanel > </ Border > </ ControlTemplate > </ Setter.Value > </ Setter > </ Style > |
This is the end of "How to customize the WP7 ListBox Selected Item | Part1: Control Template". Stay tuned with the rest of the WindowsPhoneGeek.com content and go to the second Part2 of this series "How to customize the WP7 ListBox Selected Item | Part2: Visual States".
I hope that the tip was helpful. You can download the full source code related to Part1 and Part2 of this series here:
Here are some more www.windowsphonegeek.com articles related to the ListBox Styling and customization:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述