所以我試圖將一個組合框綁定到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
}
這裏是什麼看起來像當我用C#構建教程時:
正如你所看到的,選項在C++/CX版本的空白。我究竟做錯了什麼?
XAML代碼是相關的。它需要進入你的問題。 – IInspectable
就像我說的,它基本上是從教程中複製和粘貼的。我添加它,但MainPage.xaml.h – justanotherxl