Windows Store app development tip and tricks-gridviewitem

Source: Internet
Author: User

This post talks about

* How to add itemcontainerstyleselector to gridview

* How to remove gridviewitem selection and click Animation

Itemcontainerstyleselector on msdn:

Http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.itemscontrol.itemcontainerstyleselector (V = win.10). aspx

 

Add gridviewitem styles

1. in Visual Studio 2012, create a blank Windows Store app project.

2. Open mainpage. XAML in design mode. Add a gridview.

<GridView             SelectionMode="None"            VerticalAlignment="Center"            HorizontalAlignment="Center">                        <GridView.ItemsPanel>                <ItemsPanelTemplate>                    <VirtualizingStackPanel Orientation="Horizontal"/>                </ItemsPanelTemplate>            </GridView.ItemsPanel>                        <GridViewItem Width="300" Height="300" Margin="10" Background="Gray" FontSize="50">red</GridViewItem>            <GridViewItem Width="300" Height="300" Margin="10" Background="Gray" FontSize="50">green</GridViewItem>
</GridView>

3. Run application, you can see both items in gray color.

 

Implement itemcontainerstyleselector

1. Firstly we create a custom style for gridviewitem to set the items background color as red.

2. In design mode, select gridviewitem with "Red" text. Right click on it, choose "Edit template"-> "edit a copy ...". In "create style resource" dialog, fill the name (key) as "gridviewitemredstyle", choose "define in" as "this document ". in mainpage. XAML, a new style "gridviewitemredstyle" is added in page. resources Section. we update the value of property "Background" from "transparent" to "Red ".

<Page.Resources>        <Style x:Key="GridViewItemRedStyle" TargetType="GridViewItem">            <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>            <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>            <Setter Property="Background" Value="Red"/>      …………

3. Create a new style with name "gridviewitemgreenstyle" similar with the step 2. Set the value of property "background" as "green". Remove background settings from gridviewitems, The XAML looks lik

<GridViewItem Width="300" Height="300" Margin="10" FontSize="50" Style="{StaticResource GridViewItemRedStyle}">red</GridViewItem>
<GridViewItem Width="300" Height="300" Margin="10" FontSize="50" Style="{StaticResource GridViewItemGreenStyle}">green</GridViewItem>

4. Run application. You can see the items in red and green.

5. Add new class named "mainitemcontainerstyleselector. CS" into project. Copy the below code to the class. You also need to add references.

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
public class MainItemContainerStyleSelector : StyleSelector    {        public Style RedStyle { get; set; }        public Style GreenStyle { get; set; }        protected override Style SelectStyleCore(object item, DependencyObject container)        {            string content = ((GridViewItem)item).Content.ToString();            switch(content)            {                case "red":                    return RedStyle;                    break;                case "green":                    return GreenStyle;                    break;            }            return ((GridViewItem)item).Style;        }    }

6. In mainpage. XAML, add mainitemcontainerstyleselector as staticresource.

<local:MainItemContainerStyleSelector x:Key="mainItemContainerStyleSelector" RedStyle="{StaticResource GridViewItemStyle1}" GreenStyle="{StaticResource GridViewItemStyle2}"/>

7. Update the gridview as below.

<GridView             SelectionMode="None"            VerticalAlignment="Center"            HorizontalAlignment="Center"            ItemContainerStyleSelector="{StaticResource mainItemContainerStyleSelector}">            <GridView.ItemsPanel>                <ItemsPanelTemplate>                    <VirtualizingStackPanel Orientation="Horizontal"/>                </ItemsPanelTemplate>            </GridView.ItemsPanel>                        <GridViewItem Width="300" Height="300" Margin="10" FontSize="50">red</GridViewItem>            <GridViewItem Width="300" Height="300" Margin="10" FontSize="50">green</GridViewItem>
</GridView>

8. Launch the application.

Remove selection and click animations from gridviewitem Style

1. sometimes we only want user to view the content of item, but cannot action it. in order to do this, we can update gridviewitem's style to remove selection and click animations. saying the red gridviewitem we created in the above steps. in "gridviewitemredstyle", find the visualstate "pointerover" and "pointeroverpressed ". remove storyboard from them.

2. Rebuild and launch the application, move the pointer to red item and click on it. comparing to the green item, the animations are removed from the red item now.

Source code: Http://files.cnblogs.com/Tealcwu/ItemContainerStyleSelectorDemo.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.