3

Some items in the ListView control will be selectable and have normal text. Some items however, although included in the ListView as items, will be unselectable/unclickable and 'greyed-out'.

In Windows-Store-Apps we have the ability to select Single/Multiple/None items in a ListView. But how can make certain items at certain indexes unselectable/unclickable and 'greyed-out', in code mainly?

I managed to access the Item of the ListView at a certain index:

myListView.ItemContainerGenerator.ContainerFromIndex(i)

But I couldn't find any option to customize its selected event handler. Any idea how to achieve that?

yalematta
  • 1,389
  • 1
  • 21
  • 36

2 Answers2

1

In Single selection mode. First Add a boolean property to class of binding type which defines which items are clickable like this

  class TestClass
  {
    Boolean IsClickAllowed{get;set;}
    string name{get;set;}
  }

then create a source list of TestClass type and set it as itemssource of Listview like this

var TempList=new List<>()
                    {
                        new TextClass(){IsClickAllowed=false,name="First Item"},
                        new TextClass(){IsClickAllowed=true,name="Second Item"},
                        new TextClass(){IsClickAllowed=false,name="Third Item"},
                    };
                    MyList.ItemsSource=TempList;

and for greying out Set Different DataTemplate for nonClickable items implementing DataTemplateSelector and finally for click handle in ItemClick event. You need to set IsItemClickEnabled as true.

private void MyList_ItemClick(object sender, ItemClickEventArgs e)
        {
            var item = e.ClickedItem as TestClass;
            if (item != null){
if(item.IsClickAllowed){
//Do Stuff here
}else
{
//Do Nothing
}
        }}

Hope it helps.

Rohit
  • 552
  • 3
  • 15
1

I have found a solution:

I have override the ListView control and create a StripedListView. Then by overriding the PrepareContainerForItemOverride, which is responsible for the setting up the ListViewItem control after it’s be created, you could modify the background color and set the ItemListView.isEnabled option to false:

public class StripedListView : ListView
    {          
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
            var listViewItem = element as ListViewItem;
            if (listViewItem != null)
            {
                var index = IndexFromContainer(element);

                if (Words.arrayW[index].Length > 0)
                {
                    listViewItem.Foreground = new SolidColorBrush(Colors.Black);

                }
                else
                {
                    listViewItem.Foreground = new SolidColorBrush(Colors.Gray); 
                    listViewItem.IsEnabled = false;
                }
            }
        }
    }

In Xaml:

<controls:StripedListView x:Name="letterListView" ItemsSource="{Binding}">   
      <controls:StripedListView.ItemTemplate>  
         <DataTemplate>                           
                etc...              
         </DataTemplate>
      </controls:StripedListView.ItemTemplate>
</controls:StripedListView>
yalematta
  • 1,389
  • 1
  • 21
  • 36