我已经问过这个话题,但是这个简单的绑定只是不想工作。我的代码首先:
CItem.h
#pragma once
#include "pch.h"
namespace XamlApp
{
public ref class CItem sealed
{
public:
CItem(Platform::String^ Ho, Platform::String^ Ip, Platform::String^ Im);
Platform::String^ getHo();
Platform::String^ getIP();
property Platform::String^ Ho {
Platform::String^ get() { return this->ho; }
}
property Platform::String^ Ip {
Platform::String^ get() { return this->ip; }
}
property Platform::String^ Im {
Platform::String^ get() { return this->im; }
}
public:
private:
Platform::String^ ho;
Platform::String^ ip;
Platform::String^ im;
private:
};
}
CItem.cpp:
#include "pch.h"
#include "CItem.h"
CItem::CItem(Platform::String^ Ho, Platform::String^ Ip, Platform::String^ Im) :
ho{ Ho }, ip{ Ip }, im{ Im }
{
}
Platform::String^ CItem::getHo() {
return this->ho;
}
Platform::String^ CItem::getIP() {
return this->ip;
}
主页:
Windows::UI::Xaml::Interop::IBindableVector^ test;
test = ref new Platform::Collections::Vector<CItem^>();
CItem^ dummy1 = ref new CItem(L"ho1",L"23323",L"Assets/ic_info.png");
CItem^ dummy2 = ref new CItem("ho2", "23323", "Assets/ic_info.png");
test->Append(dummy1);
test->Append(dummy2);
mainListView->ItemsSource = test;
MainPage.xaml:
<ListView x:Name="mainListView" HorizontalAlignment="Stretch" MaxWidth="500" VerticalAlignment="Center" Margin="20,0,20,-38" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Im}"/>
<TextBlock Text="{Binding Ho}"
Margin="20,0,20,8"
FontSize="24"
FontStyle="Italic"
FontWeight="SemiBold"
Foreground="DarkBlue"
/>
<TextBlock Text="{Binding Ip}"
Margin="20,0,20,8"
FontSize="24"
FontStyle="Italic"
FontWeight="SemiBold"
Foreground="DarkBlue"
/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我的问题是:此代码有什么问题?我实际上得到2个listview条目,但没有数据,它们只是空的。我已经尝试在xaml中将ho更改为Ho,我已经从microsoft-sample中逐步复制了它。
最佳答案
在最初的实现中,您需要将BindableAttribute添加到CItem类:
[Windows::UI::Xaml::Data::Bindable]
public ref class CItem sealed
资料来源:https://msdn.microsoft.com/en-us/magazine/dn166929.aspx
如果这样做,向量的类型也不必是
Windows::UI::Xaml::Interop::IBindableVector^ test;
您可以简单地使用
Platform::Collections::Vector<CItem^>^ test;
至少这对我有用!