gpt4 book ai didi

c++ - UWP - ListView 绑定(bind)不显示在 C++/CX 中的 XAML 上

转载 作者:行者123 更新时间:2023-11-28 05:01:04 25 4
gpt4 key购买 nike

我已经问过这个话题了,但是这个简单的绑定(bind)就是不想工作。首先是我的代码:

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;

主页.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

如果你这样做,你的 vector 类型也不必是

Windows::UI::Xaml::Interop::IBindableVector^ test;

你可以简单地使用一个

Platform::Collections::Vector<CItem^>^ test;

至少这对我有用!

关于c++ - UWP - ListView 绑定(bind)不显示在 C++/CX 中的 XAML 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45957176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com