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

时间:2017-02-09 04:14:19

标签: xaml uwp win-universal-app windows-10-universal c++-cx

所以我试图将组合框绑定到myclass的向量。 myclass有一个名为key的公共Platform :: String ^属性。所以在我的XAML中,我将组合框的displaymemberpath设置为“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 
}

以下是我所看到的: enter image description here

以下是我在C#中构建教程时的样子:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

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

请检查以下代码:

[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文档。

相关问题