2017-02-09 32 views
0

所以我試圖將一個組合框綁定到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#中使用原始教程,它工作得很好)。如何在UWP C++/CX的組合框中使用displaymemberpath?

這裏是我的代碼:

(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版本的空白。我究竟做錯了什麼?

+0

XAML代碼是相關的。它需要進入你的問題。 – IInspectable

+0

就像我說的,它基本上是從教程中複製和粘貼的。我添加它,但MainPage.xaml.h – justanotherxl

回答

0

如果您使用{}綁定,你需要的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文檔。

+0

以上工作,謝謝。 displaymembersource屬性在幕後還是基本上使用{Binding}? {x:Bind}是否需要該標籤? – justanotherxl

相關問題