【问题标题】:How to use displaymemberpath on a combobox in UWP C++/CX?如何在 UWP C++/CX 的组合框中使用 displaymemberpath?
【发布时间】:2017-06-26 22:57:20
【问题描述】:

所以我正在尝试将组合框绑定到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 
}

这是我所看到的:

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

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

【问题讨论】:

  • XAML 代码是相关的。它需要进入你的问题。
  • 就像我说的,它基本上是从教程中复制和粘贴的。我在 MainPage.xaml.h 上方添加了它

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


【解决方案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文档。

【讨论】:

  • 成功了,谢谢。 displaymembersource 属性基本上是在幕后使用 {Binding} 还是什么? {x:Bind} 需要该标签吗?
猜你喜欢
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 2018-08-21
相关资源
最近更新 更多