UWP - 在 C++ 中将 Vector 绑定到 ComboBox

UWP - Bind a Vector to a ComboBox in c++

我的 UI 有一个组合框。在我的网络 class 中,我有一种方法可以接收 udp 数据包并从中获取 IP 地址。我采用哪种数据类型将地址保存为字符串(Vector、IVector?)。我如何将这个具有地址的对象连接到 UI 中的组合框 - 因此每个地址都动态显示在组合框中。 我为网络 class 使用 c++,为 UI 使用 xaml + c++。为避免混淆,我使用的是 Visual Studio 2017 年的 UWP-XAML-C++ 模板。

Which datatype do I take for saving the addresses as strings (Vector, IVector ?)

是的,Vector can be used as the collection binding to the ComboBox. More details about C++/CX collection please reference this document

And how do I connect this object ,which has the addresses

为此我们需要使用 data binding

例如假设有一个networkclass包含IpAddress属性如下:

MainPage.xaml.h

中的代码
public ref class MainPage sealed
{
public:
    MainPage();
private:
    void btnbinding_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
};

public ref class network sealed
{
public:
    property Platform::String^ IpAddress;
};

然后我们可以将 network 集合(向量)绑定到 ComboBox,如下所示:

XAML代码

<ComboBox x:Name="combo" >
    <DataTemplate>
        <TextBlock Text="{Binding IpAddress}"></TextBlock>
    </DataTemplate>
</ComboBox>

代码隐藏

void CombboxC::MainPage::btnbinding_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    Platform::Collections::Vector<network^>^ items = ref new Platform::Collections::Vector<network^>();  
    network^ onenet = ref new network();
    onenet->IpAddress = "Test";
    items->Append(onenet);
    combo->ItemsSource = items; 
}