locked
C# windows store app how to select ListView template based on item index RRS feed

  • Question

  • Hello,

    I know how to select data template by the item. But how to select the data template on the item index?

    Thanks. 

    Friday, August 2, 2013 4:47 PM

Answers

  • You can use StyleSelector to implement the custom styling. Try this -

    xaml

     <Page.Resources>
            <local:CustomStyleSelecter x:Key="customStyleSelector"/>
        </Page.Resources>


     <ListView x:Name="gridView" ItemsSource="{Binding Items}" 
                          ItemContainerStyleSelector="{StaticResource customStyleSelector}"
                          BorderBrush="Cyan" BorderThickness="3">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                                <StackPanel Background="{Binding Highlight}">
                                    <TextBlock Text="{Binding Txt}" HorizontalAlignment="Stretch"/>
                                </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

    cs

      class CustomStyleSelecter : StyleSelector
        {
            protected override Style SelectStyleCore(object item, DependencyObject container)
            {
                SolidColorBrush highlight = (item as ListVwItem).Highlight;
                Style style = new Style(typeof(ListViewItem));
                style.Setters.Add(new Setter(Control.BackgroundProperty, highlight));
                return style;
            }
        }
    
        class ListVwItem
        {
            static int _itemIndex = -1;
            int _Index = -1;
            public int Index 
            {
                get
                {
                    if (_Index == -1)
                    {
                        _Index = ++_itemIndex;
                    }
                    return _itemIndex;
                }
            }
            public Uri ImageUri { get; set; }
            public string Txt { get; set; }
            public SolidColorBrush Highlight 
            {
                get
                {
                    if (Index % 2 == 0)
                       return new SolidColorBrush(Colors.Blue);
                    else
                       return new SolidColorBrush(Colors.Violet);
                }
            }
        }
    
        class ListVwModel
        {
            public List<ListVwItem> Items { get; set; }
    
            public ListVwModel()
            {
                 //Dummy Data
                Items = new List<ListVwItem>();
                Items.Add(new ListVwItem() { Txt = "1" });
                Items.Add(new ListVwItem() { Txt = "2" });
                Items.Add(new ListVwItem() { Txt = "3" });
                Items.Add(new ListVwItem() { Txt = "4" });
                Items.Add(new ListVwItem() { Txt = "5" });
                Items.Add(new ListVwItem() { Txt = "6" });
                Items.Add(new ListVwItem() { Txt = "7" });
                Items.Add(new ListVwItem() { Txt = "8" });
                Items.Add(new ListVwItem() { Txt = "9" });
            }
        }
    
        
    
        public sealed partial class WrtPage : Page
        {
            public WrtPage()
            {
                this.InitializeComponent();
                this.DataContext = new ListVwModel();
    
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
    
            }
        }


    Thanks,
    Sachin
    My Samples

    • Marked as answer by encoderuser Sunday, August 4, 2013 8:12 PM
    Sunday, August 4, 2013 5:13 AM

All replies

  • Any ideas?

    Basically I want to show the list items with odd indexed items having the same style, and even indexed items sharing another style.

    Thanks.

    Saturday, August 3, 2013 6:21 PM
  • You can use StyleSelector to implement the custom styling. Try this -

    xaml

     <Page.Resources>
            <local:CustomStyleSelecter x:Key="customStyleSelector"/>
        </Page.Resources>


     <ListView x:Name="gridView" ItemsSource="{Binding Items}" 
                          ItemContainerStyleSelector="{StaticResource customStyleSelector}"
                          BorderBrush="Cyan" BorderThickness="3">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                                <StackPanel Background="{Binding Highlight}">
                                    <TextBlock Text="{Binding Txt}" HorizontalAlignment="Stretch"/>
                                </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

    cs

      class CustomStyleSelecter : StyleSelector
        {
            protected override Style SelectStyleCore(object item, DependencyObject container)
            {
                SolidColorBrush highlight = (item as ListVwItem).Highlight;
                Style style = new Style(typeof(ListViewItem));
                style.Setters.Add(new Setter(Control.BackgroundProperty, highlight));
                return style;
            }
        }
    
        class ListVwItem
        {
            static int _itemIndex = -1;
            int _Index = -1;
            public int Index 
            {
                get
                {
                    if (_Index == -1)
                    {
                        _Index = ++_itemIndex;
                    }
                    return _itemIndex;
                }
            }
            public Uri ImageUri { get; set; }
            public string Txt { get; set; }
            public SolidColorBrush Highlight 
            {
                get
                {
                    if (Index % 2 == 0)
                       return new SolidColorBrush(Colors.Blue);
                    else
                       return new SolidColorBrush(Colors.Violet);
                }
            }
        }
    
        class ListVwModel
        {
            public List<ListVwItem> Items { get; set; }
    
            public ListVwModel()
            {
                 //Dummy Data
                Items = new List<ListVwItem>();
                Items.Add(new ListVwItem() { Txt = "1" });
                Items.Add(new ListVwItem() { Txt = "2" });
                Items.Add(new ListVwItem() { Txt = "3" });
                Items.Add(new ListVwItem() { Txt = "4" });
                Items.Add(new ListVwItem() { Txt = "5" });
                Items.Add(new ListVwItem() { Txt = "6" });
                Items.Add(new ListVwItem() { Txt = "7" });
                Items.Add(new ListVwItem() { Txt = "8" });
                Items.Add(new ListVwItem() { Txt = "9" });
            }
        }
    
        
    
        public sealed partial class WrtPage : Page
        {
            public WrtPage()
            {
                this.InitializeComponent();
                this.DataContext = new ListVwModel();
    
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
    
            }
        }


    Thanks,
    Sachin
    My Samples

    • Marked as answer by encoderuser Sunday, August 4, 2013 8:12 PM
    Sunday, August 4, 2013 5:13 AM
  • Great, this works. Thanks a lot Sachin!
    Sunday, August 4, 2013 8:12 PM