如何禁用选择 GridView 中的单个项目 [英] How to disable selection a single item in a 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屋!