How to disable selection a single item in a GridView

windows-8, windows-runtime, winrt-xaml

Solution

While I haven't done this, you should be able to use an ItemContainerStyleSelector on the GridView, the method gives you the container (GridViewItem) and the item you're binding to. From there you can set the IsEnabled property on the GridViewItem to false which makes it unselectable.

You'll also probably need to select a custom style as well since the default GridViewItem style will customise how a disabled item will look.

Update DataTemplateSelector Solution

public class IssueGridTemplateSelector : DataTemplateSelector
{
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        var selectorItem = container as SelectorItem;

        if (item is Issue)
            return IssueTemplate;

        selectorItem.IsEnabled = false;
        selectorItem.Style = RepositoryItemStyle;

        return RepositoryTemplate;
    }

    public DataTemplate IssueTemplate
    {
        get;
        set;
    }

    public DataTemplate RepositoryTemplate
    {
        get;
        set;
    }

    public Style RepositoryItemStyle
    {
        get;
        set;
    }
}

Problem

How do you disable the selection single item from a GridView? I have a GridView with it's ItemsSource bound to an IEnumerable<SampleDataItem>. I'd like to be able to programmatically not allow the selection of some items in the list while allowing selection of the others.

Original source