先上图吧:
为了便于看清楚,我的MyGridView的SelectionMode= "Multiple".可以可按到第二个选项是灰色的,不能点击了。
1. 首先,在你的数据源中添加一个bool类型的标识,假设我们的数据源为DataItem类型:
1 1 public ref class DataItem
2 2 {
3 3 public:
4 4 DataItem();
5 5 property bool Disabled;
6 6 };
2. 重新写一个新类,基类为GridView,重写它的
PrepareContainerForItemOverride方法
1 public ref class MyGridView sealed: GridView
2 {
3 public:
4 protected:
5 virtual void PrepareContainerForItemOverride( DependencyObject^ element,Platform::Object^ item ) override
6 {
7 GridView::PrepareContainerForItemOverride(element, item);
8 //获得数据
9 auto viewMode = (DataItem^)(item);
10 //可以转换成UIElement
11 UIElement^ uiElement = safe_cast<UIElement^>(element);
12 //也可以转换成GridViewItem类型,为了使用IsEnable属性,我们暂时转换为GridViewItem类型
13 GridViewItem^ gridViewItem = safe_cast<GridViewItem^>(element);
14 //设置该项的GridView的IsEnable属性
15 gridViewItem->IsEnabled = viewMode->Disabled;
16
17 //下面的代码是实现一个可变尺寸的GridView
18 Binding^ colBinding = ref new Binding();
19 colBinding->Source = viewMode;
20 colBinding->Path = ref new PropertyPath(this->ColumnSpanProperty);
21 BindingOperations::SetBinding(uiElement,Windows::UI::Xaml::Controls::VariableSizedWrapGrid::ColumnSpanProperty, colBinding);
22
23 Binding^ rowBinding = ref new Binding();
24 rowBinding->Source = viewMode;
25 rowBinding->Path = ref new PropertyPath(this->RowSpanProperty);
26 BindingOperations::SetBinding(uiElement, Windows::UI::Xaml::Controls::VariableSizedWrapGrid::RowSpanProperty, rowBinding);
27 }
28 }
29 };
然后只要在跟我们的MyGridView绑定的数据源中设置Disabled属性就可以了
MainPage::MainPage()
{
//保存了一个DataItem的Vector
DataItemVector^ databox = ref new DataItemVector();
for(int i = 0; i < 10; i++)
{
DataItem^ myData = ref new DataItem();
if(i == 1)
myData->Disabled = true;
databox->ItemVector->Append(myData);
}
//MyGridView的对象,在XAML文件中初始化的
myGridView->ItemSource = databox;
}
posted on 2013-01-10 11:02
Dino-Tech 阅读(1338)
评论(0) 编辑 收藏 引用 所属分类:
Windows 8