积极答复者
请教Metro win8 C++开发控件binding使用问题

问题
-
尝试使用binding方式绑定到对象集合,显示不出数据,但项数确实和集合总数一致。代码如下:
定义测试类:
ref class Test sealed: Platform::Object
{
public:
Test(Platform::String^ name, Platform::String^ attri, int32 count);
~Test(void);
property Platform::String^ name;
property Platform::String^ attri;
property int32 count;
};xaml里使用的控件如下:
<ComboBox x:Name="ComboBox1" ItemsSource="{Binding}" Foreground="Black" FontSize="30" Height="50" Width="450">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="Name:" Margin="2" />
<TextBlock Text="{Binding Path=name}" Margin="2" />
<TextBlock Text="Atrri:" Margin="10,2,0,2" />
<TextBlock Text ="{Binding Path=attri}" Margin="2" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>在MainPage.xaml.cpp的MainPage::MainPage()里添加代码
Platform::Collections::Vector<Test^>^ test = ref new Platform::Collections::Vector<Test^>();
test->Append(ref new Test("Sandy","female",1));
test->Append(ref new Test("Lisa","female",2));
test->Append(ref new Test("Carter","female",3));
ComboBox1->DataContext = test;显示的结果,是ComboBox有三个下拉选项,每项都是Name:Attri:,请各位前辈指教一下。
答案
-
你的代码有2个问题:
1.在声明Test class前应该设置它的bindable->[Windows::UI::Xaml::Data::Bindable]
2.另外Test class必须是public->public ref class Test sealed: Platform::Object
[Windows::UI::Xaml::Data::Bindable] public ref class Test sealed: Platform::Object { public: Test(Platform::String^ pName, Platform::String^ pAttri, int32 count){ name=pName; attri=pAttri; } ~Test(void){}; property Platform::String^ name; property Platform::String^ attri; property int32 count; };
- 已编辑 pjfitren 2012年7月19日 8:00
- 已建议为答案 Jie BaoModerator 2012年7月19日 8:23
- 已标记为答案 Jie BaoModerator 2012年7月20日 5:36
全部回复
-
你的代码有2个问题:
1.在声明Test class前应该设置它的bindable->[Windows::UI::Xaml::Data::Bindable]
2.另外Test class必须是public->public ref class Test sealed: Platform::Object
[Windows::UI::Xaml::Data::Bindable] public ref class Test sealed: Platform::Object { public: Test(Platform::String^ pName, Platform::String^ pAttri, int32 count){ name=pName; attri=pAttri; } ~Test(void){}; property Platform::String^ name; property Platform::String^ attri; property int32 count; };
- 已编辑 pjfitren 2012年7月19日 8:00
- 已建议为答案 Jie BaoModerator 2012年7月19日 8:23
- 已标记为答案 Jie BaoModerator 2012年7月20日 5:36