如何禁用选择 GridView 中的单个项目 [英] How to disable selection a single item in a GridView

查看:21
本文介绍了如何禁用选择 GridView 中的单个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用 GridView 中的单项选择?

How do you disable the selection single item from a GridView?

我有一个 GridView,它的 ItemsSource 绑定到一个 IEnumerable.我希望能够以编程方式不允许选择列表中的某些项目,同时允许选择其他项目.

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.

推荐答案

虽然我还没有这样做,但您应该能够在 GridView 上使用 ItemContainerStyleSelector,该方法为您提供容器 (GridViewItem) 和您需要的项目绑定到.从那里您可以将 GridViewItem 上的 IsEnabled 属性设置为 false,这使其无法选择.

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.

您可能还需要选择自定义样式,因为默认的 GridViewItem 样式将自定义禁用项的外观.

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.

更新 DataTemplateSelector 解决方案

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;
    }
}

这篇关于如何禁用选择 GridView 中的单个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆