简体   繁体   English

UWP-将向量绑定到C ++中的ComboBox

[英]UWP - Bind a Vector to a ComboBox in c++

I've got a combobox im my UI. 我的界面上有一个组合框。 In my network class I've got a method which recieves udp-packets and takes the IPaddresses from them. 在我的网络课程中,我有一种方法可以接收udp数据包并从中获取IP地址。 Which datatype do I take for saving the addresses as strings (Vector, IVector ?). 我采用哪种数据类型将地址保存为字符串(Vector,IVector?)。 And how do I connect this object ,which has the addresses, to my combobox in the UI - so each address is shown dynamically in the combobox . 以及如何将该具有地址的对象连接到UI中的组合框-因此,每个地址都会在组合框中动态显示。 I am using c++ for the network class and xaml + c++ for the UI. 我将c ++用于网络类,将xaml + c ++用于UI。 To avoid confusion, I am using the UWP-XAML-C++ template from Visual Studio 2017. 为避免混淆,我使用了Visual Studio 2017中的UWP-XAML-C ++模板。

Which datatype do I take for saving the addresses as strings (Vector, IVector ?) 我应采用哪种数据类型将地址另存为字符串(Vector,IVector?)

Yes, Vector can be used as the collection binding to the ComboBox . 是的, Vector可用作绑定到ComboBox的集合。 More details about C++/CX collection please reference this document . 有关C ++ / CX集合的更多详细信息,请参考此文档

And how do I connect this object ,which has the addresses 以及如何连接具有地址的对象

For this we need to use data binding . 为此,我们需要使用数据绑定

For example, suppose there is a network class contains IpAddress property as follows: 例如,假设有一个包含IpAddress属性的network类,如下所示:

Code in MainPage.xaml.h 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;
};

Then we can bind the network collection(vector) to the ComboBox as follows: 然后,我们可以将network collection(vector)绑定到ComboBox ,如下所示:

XAML Code XAML代码

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

Code behind 后面的代码

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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM