locked
The datatemplate in itemcontainerstyle no longer works in 8.1? RRS feed

All replies

  • Hi Gaurav,

    Can you please provide a sample demonstrating the behavior?

    -Sagar

    Tuesday, September 24, 2013 8:38 AM
  • Here's the Windows8 sample: http://sdrv.ms/1gW1nVY

    Copied ListView's default ItemContainerStyle and removed the references to "contentPresenter". Instead of using ItemTemplate, I've directly added the DataTemplate (i.e. StackPanel and couple of TextBlock's) to ItemContainterStyle. Also, I can now simply change the textblock foreground in "Selected" visual state.

    The bindings work correctly in Windows 8. But, once you retarget solution to Windows 8.1, it stops working.

    Tuesday, September 24, 2013 12:57 PM
  • Any updates on this?
    Wednesday, October 2, 2013 1:38 PM
  • Sorry for the delay. 

    Any specific reason why DataTemplate used in ItemContainerStyle ? Maybe you can bind that as ItemTemplate and give it try if that works for you.

    -Sagar


    Thursday, October 3, 2013 6:43 PM
  • Using this technique, one could easily access the sub-controls in DataTemplate and change their properties in PointerOver, Selected and other visual states through XAML only. Since, WinRT doesn't support triggers.

    Secondly, I'd just like to understand the reason behind this change. Why it worked in Windows 8 and not in 8.1. I hope it would help in future.

    Friday, October 4, 2013 10:56 AM
  • Hi,

    I haven't seen a lot of developers using DataTemplate inside a ControlTemplate and I am still working on it - but you can achieve the same things (and more) easily by going through the ListView/GridViewItemPresenter classes for Windows 8.1  - than fully re-templating the control.

    -Sagar

    Thursday, October 10, 2013 2:30 PM
  • Thanks for the reply. Can you please share a sample where one could easily change the properties of controls inside DataTemplate on PointerOver and Selected states? 

    Thursday, October 10, 2013 6:05 PM
  • There is *no* similar scenario in above samples. They're simply changing the properties of ListViewItemPresenter. I'm looking to modify properties of controls inside DataTemplate on PointerOver or Selected state.

    Friday, October 11, 2013 11:51 AM
  • Hi,
    I reviewed the XAMl code and saw that you are changing the Foreground property of the TextBlock in the DataTemplate when some selection occurs - With the ListViewItemPresenter, you can do this by using just one property - SelectedForeground 

    Similarly if you look closely at the  XAML ListView and GridView essentials sample (Windows 8.1)  scenario 4 code, you should be able to view most of the properties like   FocusBorderBrush , PlaceholderBackground , PointerOverBackground , SelectedBorderThickness ,  SelectedBackground , SelectedForeground , SelectedPointerOverBackground , SelectedPointerOverBorderBrush  - which can be used instead of the ControlTemplate.

    I used the ItemContainerStyle from the scenario 4 - called "CustomizedGridViewItemPresenterTemplate" and applied to your sample as :

    <Style x:Key="CustomizedGridViewItemPresenterTemplate" TargetType="ListViewItem">
                <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
                <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="TabNavigation" Value="Local"/>
                <Setter Property="IsHoldingEnabled" Value="True"/>
                <Setter Property="Margin" Value="0,0,2,2"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <ListViewItemPresenter
                            ContentTransitions="{TemplateBinding ContentTransitions}"
                            Margin="{TemplateBinding Padding}"
                            SelectionCheckMarkVisualEnabled="True"
                            CheckHintBrush="{ThemeResource ListViewItemCheckHintThemeBrush}"
                            CheckSelectingBrush="{ThemeResource ListViewItemCheckSelectingThemeBrush}"
                            CheckBrush="{ThemeResource ListViewItemCheckThemeBrush}"
                            DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
                            DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
                            FocusBorderBrush="{x:Null}"
                            PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
                            PointerOverBackground="{StaticResource CustomizedGridViewItemPresenterHighlightBrush}"
                            SelectedBorderThickness="{StaticResource CustomizedGridViewItemPresenterSelectedBorderThickness}"
                            SelectedBackground="{StaticResource CustomizedGridViewItemPresenterHighlightBrush}"
                            SelectedForeground="Red" 
                            SelectedPointerOverBackground="{StaticResource CustomizedGridViewItemPresenterHighlightBrush}"
                            SelectedPointerOverBorderBrush="{ThemeResource ListViewItemSelectedPointerOverBorderThemeBrush}"
                            DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                            DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
                            ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}">
                                <ListViewItemPresenter.ContentTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <StackPanel Margin="10,0,0,0">
                                                <TextBlock x:Name="MyName" Text="{Binding Name}" TextWrapping="NoWrap" MaxHeight="40"/>
                                                <TextBlock x:Name="MyNumber" Text="{Binding Number}" TextWrapping="NoWrap"/>
                                            </StackPanel>
                                        </Grid>
                                    </DataTemplate>
                                </ListViewItemPresenter.ContentTemplate>
                            </ListViewItemPresenter>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    This way , you are able to define a DataTemplate and also make changes to it on certain events like PointerOver, Selected , etc. 

    -Sagar

    Friday, October 11, 2013 6:34 PM
  • What if one has to change the FontSize of each TextBlock to a different value on PointerOver, Selected, etc. states?

    I'm not sure if the above method will apply here.

    Thank you for the help.  


    • Edited by gaurav77 Tuesday, October 15, 2013 5:11 PM
    Saturday, October 12, 2013 7:20 AM
  • What if one has to change the FontSize of each TextBlock to a different value on PointerOver, Selected, etc. states?

    I'm not sure if the above method will apply here.

    Thank you for the help.  

    Tuesday, October 15, 2013 5:11 PM