Multiple Flyout Templates for ComboBox and Changing the Header

Multiple Flyout Templates for ComboBox and Changing the Header

Following my previous post Breaking apart the Windows Phone 8.1 ComboBox Style and Colors a reader asked me:
– How to vary the flyout template so that each combobox could have a different appearance
– Change the header on the flyout from the default “CHOOSE AN ITEM”

The ComboBox uses the default style for the ListPickerFlyoutPresenter which means that in order to vary the template for each ComboBox you need to change the default style. Luckily you can do this in code. Take the following XAML which declares two Styles based on the same base style for the ListPickerFlyoutPresenter with the background color varying.
<Style
    x_Key=”PurpleBackgroundListPickerFlyoutPresenter”
    TargetType=”ListPickerFlyoutPresenter”
    BasedOn=”{StaticResource CustomListPickerFlyoutPresenter}”>
    <Setter
        Property=”Background”
        Value=”Purple” />
</Style>
<Style
    x_Key=”GreenBackgroundListPickerFlyoutPresenter”
    TargetType=”ListPickerFlyoutPresenter”
    BasedOn=”{StaticResource CustomListPickerFlyoutPresenter}”>
    <Setter
        Property=”Background”
        Value=”Green” />
</Style>

In order to switch between templates you just need to change the default template using the type as the key:

private void PurpleClick(object sender, RoutedEventArgs e) {
    var purple = Application.Current.Resources[“PurpleBackgroundListPickerFlyoutPresenter”];
    Application.Current.Resources[typeof (ListPickerFlyoutPresenter)] = purple;
}

private void GreenClick(object sender, RoutedEventArgs e) {
    var green = Application.Current.Resources[“GreenBackgroundListPickerFlyoutPresenter”];
    Application.Current.Resources[typeof(ListPickerFlyoutPresenter)] = green;
}

The second part is a little harder to track down because it doesn’t appear that the text “CHOOSE AN ITEM” is set anywhere but it’s being set on the TitlePresenter TextBlock in the ListPickerFlyoutPresenter template. Often by removing named items in the default template you can raise an internal exception. Luckily in this case you can simply remove the TitlePresenter element and replace it with a similar TextBlock containing the header you want. Setting the TitlePresenter to Collapsed doesn’t work as it’s set to Visible by the ComboBox so you actually need to either remove the item or set the Opacity to 0.

Breaking apart the Windows Phone 8.1 ComboBox Style and Colors

Breaking apart the Windows Phone 8.1 ComboBox Style and Colors

Yesterday I had an interesting, if not a little frustrating, time pulling apart the default style of the Windows Phone 8.1 ComboBox. Before I start I’ve put together a series of images showing various states of the ComboBox:

A – The default unfocused state of the ComboBox, showing the placeholder text
B – The state when an item has been selected (focused and unfocused look the same)
C – The pressed state, prior to items selection showing
D – The expanded state when only a few items (5 or less) in the list using one of the default TextBlock styles (we’ll discuss this in a minute)
E – The expanded state when only a few items (5 or less) in the list using a TextBlock without the Foreground color set (no selection)
F – The expanded state when only a few items (5 or less) in the list using a TextBlock without the Foreground color set (item selected)
G – The disabled (ie IsEnabled=false) state
H – The expanded state when more than 5 items, which is same regardless of whether an item is selected or not (ie no selection shown)

image

 

Don’t Use Any of the Built In TextBlock Styles!!!

Before we jump into look at the ComboBox styles and colors in more detail, let me briefly discuss D in more detail. For this particular scenario the combobox has 3 items in it which means that the expanded view will show the items in situ (same behaviour as the ListPicker from Windows Phone 8/8.1 Silverlight). The ItemTemplate for the ComboBox looks like the following:

<DataTemplate x_Key=”ItemsItemTemplate”>
    <TextBlock Text=”{Binding Property1}” Style=”{StaticResource BodyTextBlockStyle}” />
</DataTemplate>

On face value this looks great – we have a simple TextBlock which is data bound and we’re using one of the out of the box styles, BodyTextBlockStyle. Unfortunately this inherits from BaseTextBlockStyle which sets the Foreground based on the current theme, as the following XAML snippet illustrates.

<Style x_Key=”BaseTextBlockStyle” TargetType=”TextBlock”>
    …….
    <Setter Property=”Foreground” Value=”{ThemeResource PhoneForegroundBrush}”/>
    …….
</Style>

<Style x_Key=”BodyTextBlockStyle” BasedOn=”{StaticResource BaseTextBlockStyle}” TargetType=”TextBlock”>
    <Setter Property=”LineHeight” Value=”24″/>
</Style>

The issue with this is that the in situ expanded view relies on Foreground being inherited from the host control, which doesn’t happen if it is explicitly set on an element (such as this case where the Foreground is being explicitly set in the TextBlock Style). The work around is simple – don’t use the built in styles, start by taking a copy of them and working with those instead eg:

<Style x_Key=”NoForegroundBaseTextBlockStyle” TargetType=”TextBlock”>
    <Setter Property=”FontFamily” Value=”{ThemeResource PhoneFontFamilyNormal}”/>
    <Setter Property=”FontSize” Value=”{ThemeResource TextStyleLargeFontSize}”/>
    <Setter Property=”TextTrimming” Value=”Clip”/>
    <Setter Property=”TextWrapping” Value=”WrapWholeWords”/>
    <Setter Property=”LineHeight” Value=”21.5″/>
    <Setter Property=”LineStackingStrategy” Value=”BaselineToBaseline”/>
    <Setter Property=”TextLineBounds” Value=”Full”/>
    <Setter Property=”OpticalMarginAlignment” Value=”TrimSideBearings”/>
</Style>
<Style x_Key=”NoForegroundTextBlockStyle” BasedOn=”{StaticResource NoForegroundBaseTextBlockStyle}” TargetType=”TextBlock”>
    <Setter Property=”LineHeight” Value=”24″ />
</Style>

When I use the NoForegroundTextBlockStyle instead of D, when I expand the ComboBox I see E (no item selected) or F (item selected) instead. Note how they pick up the foreground colour, including the accent colour for the selected item.

 

Now for the Colors

You’d have thought that adjusting the colors throughout the combobox would be relatively straight forward. Unfortunately this is not the case but let’s start with what we can adjust without tweaking the Template of the ComboBox. Selecting the ComboBox and looking at the Properties window (VS or Blend) I can adjust the Background, Foreground and BorderBrush, as shown in the following image where I’ve set each of these to a distinct color so that we can see where they appear in the ComboBox:

image

The impact on the ComboBox is as follows (see image below):

1 – In the unselected state, the Foreground color has no impact on the color of the placeholder text but the background and border colors can clearly be seen
2 & 3 – In the selected and pressed states, again, the Foreground color has no impact
4 – In the expanded state there are no color changes
5 – In the selected state, when there are 5 or fewer items, the Foreground color is evident in the text
6 – No change to the disabled state
7 – The Foreground color is used to highlight the selected item

image

Clearly the use of these three colors hasn’t been applied consistently through the ComboBox states so we’re going to have to dig deeper in order to tweak things. I’ll start by looking at the Template for the ComboBox by right-clicking on the ComboBox, selecting Edit Template, Edit a Copy.

image

There are three components of the Template being the presenter for the header, a button and the border which will house the expanding list for when there are five or fewer items.

image

The Button is what is displayed when there are 5 or more items. Selecting the Button we can see that both Background and BorderBrush are set but the Foreground is inherited. I’ll update this to be data bound using Template Binding to the Foreground button.

image

We’ve sorted out the inconsistency in 1. This actually also sorted out the foreground color in 2 and 3. However, when the button is pressed, we still see the accent color coming through, which in most cases conflicts with the branding of our app. The question is what to replace it with…. this is an issue for another day; for now, let’s just change it to use a different static resource. Right-click on the button, select Edit Template, Edit a Copy, which will take a copy of the button template and put Blend into template editing mode. From the States window, select the Pressed state.

image

In the Objects and Timeline we can see that the Background on the Border element has been set to the accent color. I’ll select the Border and from the Properties window select Convert to New Resource from the Background.

image

I’ll name the new resource ComboBoxPressedBrush and set the color to Pink so it stands out.

Solving 4 includes fixing a few things: Background, Border, Foreground and Selected Foreground. First things first, let’s change the white background to the background set on the ComboBox. Looking at the template it would appear that the background is already set using data binding through Template binding to the Background being set on the ComboBox. However, at least two of the States in the template are adjusting the background – in fact the error shown in Blend is the result of some developer not understanding how visual state groups work, or being too lazy to establish a working set of states. Anyhow, what we want to do in this case is actually to adjust both states to remove any changes to the background property.

image

In this case we need to remove the Background property from the following states:

CommonStates – Pressed
CommonStates – Disabled
CommonStates – Highlighted
DropDownStates – Opened

Expanding the ComboBox now shows the expanded view with the correct Background color. Now onto Foreground for both the unselected and selected items. The Foreground color of the selected item is actually set correctly – it makes sense for this to be the Foreground color set on the ComboBox. However, the unselected Foreground color is currently set to a theme color, ComboBoxHighlightedForegroundThemeBrush. To override this color you can simply define another brush resource with the same name eg:

<SolidColorBrush x_Key=”ComboBoxHighlightedForegroundThemeBrush” Color=”#FF00F3FF”/>

We do also want to address the conflicting visual states by removing the Foreground property from the DropDownStates – Opened. Also, remove the BorderBrush from the CommonStates – Highlighted state, which will fix the border on the expanded area. That solves number 4.

5 requires no changes

Addressing 6 requires two steps. The first is to overwrite some of the built in colors that control how the ComboBox appears when it is disabled. To do this we simply create new brush resources with the same name in our application. The following resources use the same Border, Background and Foreground that I set earlier on the ComboBox, except the Opacity is set to 40%

<SolidColorBrush x_Key=”ComboBoxDisabledBorderThemeBrush” Color=”#9900FF00″ />
<SolidColorBrush x_Key=”ComboBoxDisabledForegroundThemeBrush” Color=”#990000FF” />
<SolidColorBrush x_Key=”ComboBoxDisabledBackgroundThemeBrush” Color=”#99FF0000″ />

The other part is to adjust the Disabled state of the FlyoutButton. As I did earlier I needed to edit the Template for the Button and set the disabled BorderBrush, Background and Foreground to the corresponding ComboBoxDisabled brush.

7 requires a bit more exploration of what happens when the ComboBox expands to full screen in order to see the list of items. When this happens a flyout is displayed using the ListPickerFlyoutPresenter. The template for this control can be found in C:Program Files (x86)Windows Phone Kits8.1IncludeabiXamlDesigngeneric.xaml. Copying this template into the application resources means we can make changes, in this case to the Foreground and Background colors:

<SolidColorBrush x_Key=”ComboBoxFullScreenExpandedBackgroundThemeBrush” Color=”DarkGray” />
<SolidColorBrush x_Key=”ComboBoxFullScreenExpandedForegroundThemeBrush” Color=”Orange” />

<Style TargetType=”controls:ListPickerFlyoutPresenter”>
    <Setter Property=”Foreground” Value=”{StaticResource ComboBoxFullScreenExpandedForegroundThemeBrush}” />
    <Setter Property=”Background” Value=”{StaticResource ComboBoxFullScreenExpandedBackgroundThemeBrush}” />
    ….

The only thing this doesn’t affect is the header on the flyout page which still appears in white. To get this to use the Foreground value I just set I need to modify the FlyoutPickerTitleTextBlockStyle built in style (which is also in the generic.xaml file) by copy it into the application resources and altering the BasedOn value to use the NoForegroundBaseTextBlockStyle defined earlier.

<Style x_Key=”FlyoutPickerTitleTextBlockStyle” TargetType=”TextBlock” BasedOn=”{StaticResource NoForegroundBaseTextBlockStyle}”>
    ……
</Style>

With these changes made we’ve got much better control over how the ComboBox displays text and how the border, background and foreground colors are applied.

image

 

Hopefully in this post you’ve seen how you can jump in and alter colors throughout the ComboBox templates. There are a series of built in colors both in generic.xaml and themeresources.xaml (C:Program Files (x86)Windows Phone Kits8.1IncludeabiXamlDesign) which can be overwritten to do simple changes but sometimes you need to modify the underlying templates.

When Lists aren’t Vertical or Horizontal – Going Diagonal with Windows ListView Control

When Lists aren’t Vertical or Horizontal – Going Diagonal with Windows ListView Control

The designer for a project we’re currently working on just pitched an idea where there would be a list of items that were presented along a diagonal. Back in Silverlight this probably would have been accomplished using a PathListBox but of course that doesn’t exist in the Windows 8 world. So this got me thinking …. and actually I had to reach out to UX guru Shane Morris for some inspiration. Whilst I’m not using the viewbox suggestion he had, the solution was much easier than I initially thought and essential revolves around rotating the ListView one direction and the items in the list back the other way (so they’re still horizontal).

My first pass was literally just that. A rotate transform of 27 degrees on the ListView and then a –27 degree rotate on the first item in the ItemTemplate:

<ListView>
    <ListView.RenderTransform>
        <CompositeTransform
            Rotation="27" />
    </ListView.RenderTransform>
</ListView>

<DataTemplate
    x_Key="MyItemTemplate">
    <Grid
        RenderTransformOrigin="0.5,0.5">
        <Grid.RenderTransform>
            <CompositeTransform
                Rotation="-27" />
        </Grid.RenderTransform>
        <!–     …. –>
    </Grid>
</DataTemplate>

Unfortunately this didn’t work particularly well as it ended up with too many layout issues (see below image). There was incorrect header/footer spacing but more importantly the hover effect (ie the grey background) wasn’t being rotated.

image

My next attempt involved still rotating the ListView but instead of rotating a child of the ItemTemplate I instead rotated the ListViewItemPresenter which is found by right-clicking on the ListView and selecting Edit Additional Templates, Edit Generated Item Container (ItemContainerStyle), Edit a Copy (first time only).

image

<ListViewItemPresenter …. RenderTransformOrigin="0.5,0.5">
    <ListViewItemPresenter.RenderTransform>
        <CompositeTransform Rotation="-27"/>
    </ListViewItemPresenter.RenderTransform>
</ListViewItemPresenter>

Now we’re getting closer – the hover aligns with the item as they’re both being correctly rotated. I still had issues with the positioning on the screen to ensure items scroll all the way to the edge of the screen and that all of them can still be scrolled completely into view. Firstly, I had to adjust the position of the ListView so that after rotating the trailing corner is flush with the top and bottom of the screen.

image

Doing this will prevent the first and last item being able to be scrolled into view. This can be fixed by adding a spacer into the header and footer template of the ListView.

The last issue to overcome is that the scroll bar goes off the screen at the bottom.

image

This can be fixed by tweaking the template of the scrollviewer, setting a bottom margin to lift the vertical scrollbar back onto the screen. To get to this template you have to edit the template of the ListView (not one of the additional templates), then edit the template of the ScrollViewer (which itself is part of the ListView template).

The result is that we have items that are presented, and scroll, along a diagonal line.

image