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

标签 c++ xaml listview data-binding uwp

我已经问过这个话题了,但是这个简单的绑定(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/

相关文章:

C++ - 从基类继承,成员函数作为模板参数

c++ - 按下触摸屏会触发mousePressEvent,如何判断是鼠标点击还是触摸屏点击?

xaml - 使用 StrokeDashArray 的均匀间隔破折号

c# - 如何编辑 Storyboard以将其置于按钮样式中?

android - 来自 CustomAdapter 的 getView() 的意外行为

android - AsyncTask 和 listview - 如何避免每次都加载所有数据

c++ - 为什么 setw(n) 在这里不起作用?

c++ - 解决这个问题的时间复杂度是多少?

c# - WPF Metro UI 图表 - 负值

ListView PullToRefresh 隐藏 "Pull to refresh"字符串在上面一行下