如何在 UWP C++/CX 中的组合框上使用 displaymemberpath?

How to use displaymemberpath on a combobox in UWP C++/CX?

所以我正在尝试将组合框绑定到 myclass 的向量。 myclass 有一个名为 key 的 public Platform::String^ 属性。因此,在我的 XAML 中,我将组合框的显示成员路径设置为 "key"。但是,我只得到一个空选项框,这与我将 displaymemberpath 设置为 "asdf"(不存在的 属性)的结果相同。为了说明我的问题,我尝试重写有关在 C++/CX 中使用 displaymemberpath 的教程:https://asp-net-example.blogspot.com/2017/01/uwp-combobox-displaymemberpath-and.html(我尝试按照 C# 中的原始教程进行操作,但效果非常好)。

这是我的代码:

(MainPage.xaml和教程完全一样)

<Page
    x:Class="displaymembercxxtest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:displaymembercxxtest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel 
        x:Name="stack_panel1" 
        Orientation="Horizontal"
        Background="LightYellow"
        Padding="100"
        >
        <ComboBox 
            x:Name="ComboBox1" 
            Header="Select A Color"
            DisplayMemberPath="ColorName"
            SelectedValuePath="ColorValue"
            SelectionChanged="ComboBox1_SelectionChanged"
            >
        </ComboBox>
        <TextBlock
            x:Name="TextBlock1"
            FontFamily="Consolas"
            FontSize="25"
            Foreground="DarkGreen"
            Margin="50,5,5,5"
            />
    </StackPanel>

</Page>

隐藏代码:

//MainPage.xaml.h
namespace displaymembercxxtest
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public ref class MainPage sealed
    {
    public:
        MainPage();

    private:
        void ComboBox1_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e);
    };
    public ref class color sealed
    {
    public:
        property Platform::String^ ColorName;
        property Platform::String^ ColorValue;
        color(Platform::String^ name, Platform::String^ value)
        {
            ColorName = name;
            ColorValue = value;
        }

    };
}

//MainPage.xaml.cpp

MainPage::MainPage()
{
    InitializeComponent();
    Platform::Collections::Vector<color^>^ colors = ref new Platform::Collections::Vector<color^> ();
    colors->Append(ref new color("INDIANRED", "#CD5C5C"));
    colors->Append(ref new  color("SALMON", "#FA8072"));
    colors->Append(ref new  color("CRIMSON", "#DC143C"));
    colors->Append(ref new  color("DEEPPINK", "#FF1493"));
    colors->Append(ref new  color("CORAL", "#FF7F50"));
    colors->Append(ref new  color("ORANGE", "#FFA500"));
    colors->Append(ref new  color("YELLOW", "#FFFF00"));
    colors->Append(ref new  color("PEACHPUFF", "#FFDAB9"));
    ComboBox1->ItemsSource = colors;
}


void displaymembercxxtest::MainPage::ComboBox1_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
   // not relevant to the problem 
}

这是我看到的:

这是我用 C# 构建教程时的样子:

如您所见,C++/CX 版本中的选项是空白的。我究竟做错了什么?

如果您正在使用 {Binding},则需要将 BindableAttribute 属性添加到颜色 class。

请检查以下代码:

[Windows::UI::Xaml::Data::Bindable]
public ref class color sealed
{
public:
    property Platform::String^ ColorName;
    property Platform::String^ ColorValue;
    color(Platform::String^ name, Platform::String^ value)
    {
        ColorName = name;
        ColorValue = value;
    }
};

更多信息,请阅读Data binding overview文档。