【问题标题】:UWP - ListView binding in C++/CXUWP - C++/CX 中的 ListView 绑定
【发布时间】:2018-02-06 19:57:10
【问题描述】:

这个话题在 c# 中似乎很简单,但我需要在 c++/cx 中处理它。我想用“CItem”的列表/向量填充列表视图。所以我试过这个:

主页 XAML:

<ListView x:Name="mainListView" HorizontalAlignment="Stretch" VerticalAlignment="Center"  Margin="20,0,20,6" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Height="40" Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Border Background ="Gray"  Width="50" Height="50" >
                            <Image Source="{Binding Image}" Stretch="UniformToFill" ></Image>
                        </Border>
                        <StackPanel>
                            <TextBlock Text="{Binding Hostname}" TextWrapping="NoWrap"/>
                        </StackPanel>

                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

主页类:

Windows::UI::Xaml::Interop::IBindableVector^ test;
CItem^ item = ref new 
CItem("name", "Assets/ic_help");
test->Append(item);
mainListView->ItemsSource = test;

CItem.h:

#pragma once

#include "pch.h"


    ref class CItem sealed
    {

    private:
        Platform::String^ name;
        Platform::String^ image;


    public:

        CItem(Platform::String^ name, Platform::String^ image) :
            name {name}, image {image} {}

        property Platform::String^ Name
        {
            Platform::String^ get() { return this->name;}
        }

        property Platform::String^ Image
        {
            Platform::String^ get() { return this->image;}
        }


    };

这段代码的问题是我在 IBindableVector^ 上得到了一个空指针,因为我不知道如何初始化它。我尝试使用普通的字符串列表/向量,但它在 c++ 中不起作用。所以你有我的模板或提示我如何轻松解决这个问题。我只是想拥有一个自定义项目的容器,每次获得新数据时,我都想再次设置列表视图的 ItemSource。我想使用 ->ItemSource 进行绑定而不是 viewcontroller 模式的原因是我想稍后将 listview 传递给生成数据并更新列表的线程(就像我已经在 C# 中所做的那样,它正在工作)。

【问题讨论】:

    标签: xaml listview data-binding uwp c++-cx


    【解决方案1】:

    这段代码的问题是我在 IBindableVector^ 上得到了一个空指针,因为我不知道如何初始化它。

    IBindableVector 表示可绑定的对象的可写向量集合。您需要将IBindableVector初始化为 vector collection,例如,将IBindableVector初始化如下,您会发现绑定工作:

    Windows::UI::Xaml::Interop::IBindableVector^ test;
    test=ref new Platform::Collections::Vector<CItem^>();
    

    更多详情请参考XamlBind official sample

    【讨论】:

      【解决方案2】:

      在您的初始实现中,您需要将 BindableAttribute 添加到您的 CItem 类中:

      [Windows::UI::Xaml::Data::Bindable]
      public ref class CItem sealed
      

      Source

      如果你这样做,你的向量的类型不必是

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

      你可以简单地使用一个

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

      至少这对我有用!

      【讨论】:

        猜你喜欢
        • 2018-02-07
        • 2019-09-23
        • 1970-01-01
        • 2019-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-30
        相关资源
        最近更新 更多