如何从winui3 c++/winrt中的Combobox中获取用户选择项?

发布于 2025-01-22 16:24:17 字数 1139 浏览 0 评论 0原文

我想从Winui3 C ++/WinRT中的ComboBox中获取所选的用户项目。 XAML中有此代码:

<ComboBox x:Name="ComboTranslate" HorizontalAlignment="Center" Header="Translate to" SelectionChanged="ComboBox_SelectionChanged" SelectedIndex="0" Width="200" Margin="0,15,0,0">
        <x:String>2</x:String>
        <x:String>8</x:String>
        <x:String>10</x:String>
        <x:String>16</x:String>
</ComboBox>

在Winui3中的C#中,它实现如下:

private void ColorComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Add "using Windows.UI;" for Color and Colors.
    string colorName = e.AddedItems[0].ToString();
    Color color;
    switch (colorName)
    {
        case "Yellow":
            color = Colors.Yellow;
            break;
        case "Green":
            color = Colors.Green;
            break;
        case "Blue":
            color = Colors.Blue;
            break;
        case "Red":
            color = Colors.Red;
            break;
    }
    colorRectangle.Fill = new SolidColorBrush(color);
}

如何在C ++/WinRT上执行此操作?

I would like to get the selected user item from a ComboBox in WinUI3 C++/WinRT.
There is this code in XAML:

<ComboBox x:Name="ComboTranslate" HorizontalAlignment="Center" Header="Translate to" SelectionChanged="ComboBox_SelectionChanged" SelectedIndex="0" Width="200" Margin="0,15,0,0">
        <x:String>2</x:String>
        <x:String>8</x:String>
        <x:String>10</x:String>
        <x:String>16</x:String>
</ComboBox>

In C# in WinUI3 it is implemented as follows:

private void ColorComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Add "using Windows.UI;" for Color and Colors.
    string colorName = e.AddedItems[0].ToString();
    Color color;
    switch (colorName)
    {
        case "Yellow":
            color = Colors.Yellow;
            break;
        case "Green":
            color = Colors.Green;
            break;
        case "Blue":
            color = Colors.Blue;
            break;
        case "Red":
            color = Colors.Red;
            break;
    }
    colorRectangle.Fill = new SolidColorBrush(color);
}

How can you do this on C++/WinRT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-01-29 16:24:17

以下是我在PET项目的弹出窗口类上进行操作的方式,使用SelectedIndex()方法:

void
CAUIPopUpButton::HandleHostSelectionChangedEvent(
    Windows::Foundation::IInspectable const & /* sender */,
    Windows::UI::Xaml::Controls::SelectionChangedEventArgs const & /* event */ )
{
    M_LOG_METHOD( HandleHostSelectionChangedEvent )

    TIndex newSelectedItem;

    newSelectedItem = _hUIElement.as< Windows::UI::Xaml::Controls::ComboBox >().SelectedIndex();

    ValueChanged( newSelectedItem );
}

基本上,我在手动将事件处理程序连接到Combobox时,我并不使用发件人和事件的信息,上面的代码。

_hUIElement = Windows::UI::Xaml::Controls::ComboBox();
_hUIElement.as< Windows::UI::Xaml::Controls::ComboBox >().SelectionChanged( { this, & CAUIPopUpButton::HandleHostSelectionChangedEvent } );

请不要判断我的C ++风格:我有点老派,而不是XAML的忠实拥护者!

Here's how I'm doing it on my pet project's PopUpButton class, using the SelectedIndex() method:

void
CAUIPopUpButton::HandleHostSelectionChangedEvent(
    Windows::Foundation::IInspectable const & /* sender */,
    Windows::UI::Xaml::Controls::SelectionChangedEventArgs const & /* event */ )
{
    M_LOG_METHOD( HandleHostSelectionChangedEvent )

    TIndex newSelectedItem;

    newSelectedItem = _hUIElement.as< Windows::UI::Xaml::Controls::ComboBox >().SelectedIndex();

    ValueChanged( newSelectedItem );
}

Basically, I'm not using the information of the sender and event as I manually connect the event handler to the ComboBox, the _hUIElement in the code above.

_hUIElement = Windows::UI::Xaml::Controls::ComboBox();
_hUIElement.as< Windows::UI::Xaml::Controls::ComboBox >().SelectionChanged( { this, & CAUIPopUpButton::HandleHostSelectionChangedEvent } );

Please don't judge my C++ style: I'm a bit old school and not a big fan of XAML !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文