将基类对象绑定到windows store app(metro app)中的Gridview itemSource c ++无法正常工作

时间:2014-04-24 06:23:13

标签: visual-c++ gridview data-binding windows-runtime windows-store-apps

在我的Windows商店应用程序中,我在名为" BaseClass"的基类中实现属性更改事件。并编写我想绑定到gridview项的属性,然后继承这个基类" BaseClass"在Derived class" DerivedClass"。

[Windows::UI::Xaml::Data::Bindable]
ref class BaseClass: Windows::UI::Xaml::DependencyObject, Windows::UI::Xaml::Data::INotifyPropertyChanged
{
}

ref class DerivedClass: public BaseClass
{
}

然后我创建了一个Windows :: UI :: Xaml :: Interop :: IBindableObservableVector ^向量,并使用safe_cast从派生类对象追加基类对象。但它没有将基类数据绑定到网格视图,但是当我创建基类对象时,它可以将数据绑定到gridview。

DerivedClass^ derivedClass = ref new DerivedClass();
BaseClass^ baseClass = safe_cast<BaseClass^>(derivedClass);

Windows::UI::Xaml::Interop::IBindableObservableVector^ m_Vector = ref new Platform::Collections::Vector<BaseClass^>();
m_Vector->Append(baseClass);

gridview->ItemsSource = m_Vector; // Not binding data to gridview.

BaseClass^ baseClass2 = ref new BaseClass();
Windows::UI::Xaml::Interop::IBindableObservableVector^ m_Vector = ref new Platform::Collections::Vector<BaseClass^>();
m_Vector->Append(baseClass2);

gridview->ItemsSource = m_Vector; // its Binding data to gridview.

1 个答案:

答案 0 :(得分:0)

您可能需要将视图模型注释为bindable

[Windows::UI::Xaml::Data::Bindable]
ref class DerivedClass: public BaseClass
{

}