最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xaml - Can't get SelectedValue in WinUI 3 ComboBox to work - Stack Overflow

programmeradmin2浏览0评论

I tried this with String (hstring) combo items and everything worked fine. Now I try to use structure and... trying to fill ComboBox and select one of them. ComboBox is filled ok, but no item is selected. I'd like to select them by Locale. What I do wrong? :)

Parts of source files here. IDL file:

    [default_interface]
    runtimeclass LanguageOpt
    {
        LanguageOpt();
        String Native;
        String Locale;
    };

    [default_interface]
    runtimeclass Settings : Microsoft.UI.Xaml.Controls.Page, Microsoft.UI.Xaml.Data.INotifyPropertyChanged
    {
        Settings();

        // UI languages
        Windows.Foundation.Collections.IObservableVector<LanguageOpt> Languages{ get; };
        LanguageOpt CurrentLang;
    }

Some code (.h):

    struct Settings : SettingsT<Settings>
    {
Windows::Foundation::Collections::IObservableVector<LanguageOpt> Languages();

        LanguageOpt CurrentLang();
        void CurrentLang(LanguageOpt const& value);


    private:

        LanguageOpt m_CurrentLang;
        Windows::Foundation::Collections::IObservableVector<LanguageOpt> m_Languages;
    };

.cpp file

    Settings::Settings()
    {
        m_Languages = winrt::single_threaded_observable_vector<LanguageOpt>();
        ... // filling m_Languages

        // set current language
        m_CurrentLang.Locale(L"en-US");
        m_CurrentLang.Native(L"English");
    }

XAML:

        <ComboBox x:Name="LanguageCombo"
                  ItemsSource="{x:Bind Languages}" 
                  SelectedValuePath="Locale"
                  SelectedValue="{x:Bind CurrentLang, Mode=TwoWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate x:DataType="local:LanguageOpt">
                    <TextBlock Text="{x:Bind Native}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ComboBox>

When I try to check what's selected (after changing selection in running app) by calling CurrentLang(), I get the same value set in Settings constructor despite TwoWays mode. It looks like binding is not established :)

I tried this with String (hstring) combo items and everything worked fine. Now I try to use structure and... trying to fill ComboBox and select one of them. ComboBox is filled ok, but no item is selected. I'd like to select them by Locale. What I do wrong? :)

Parts of source files here. IDL file:

    [default_interface]
    runtimeclass LanguageOpt
    {
        LanguageOpt();
        String Native;
        String Locale;
    };

    [default_interface]
    runtimeclass Settings : Microsoft.UI.Xaml.Controls.Page, Microsoft.UI.Xaml.Data.INotifyPropertyChanged
    {
        Settings();

        // UI languages
        Windows.Foundation.Collections.IObservableVector<LanguageOpt> Languages{ get; };
        LanguageOpt CurrentLang;
    }

Some code (.h):

    struct Settings : SettingsT<Settings>
    {
Windows::Foundation::Collections::IObservableVector<LanguageOpt> Languages();

        LanguageOpt CurrentLang();
        void CurrentLang(LanguageOpt const& value);


    private:

        LanguageOpt m_CurrentLang;
        Windows::Foundation::Collections::IObservableVector<LanguageOpt> m_Languages;
    };

.cpp file

    Settings::Settings()
    {
        m_Languages = winrt::single_threaded_observable_vector<LanguageOpt>();
        ... // filling m_Languages

        // set current language
        m_CurrentLang.Locale(L"en-US");
        m_CurrentLang.Native(L"English");
    }

XAML:

        <ComboBox x:Name="LanguageCombo"
                  ItemsSource="{x:Bind Languages}" 
                  SelectedValuePath="Locale"
                  SelectedValue="{x:Bind CurrentLang, Mode=TwoWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate x:DataType="local:LanguageOpt">
                    <TextBlock Text="{x:Bind Native}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ComboBox>

When I try to check what's selected (after changing selection in running app) by calling CurrentLang(), I get the same value set in Settings constructor despite TwoWays mode. It looks like binding is not established :)

Share Improve this question asked Mar 17 at 12:52 CyberDemonCyberDemon 13 bronze badges 2
  • Not sure about the C++ stuff, but when the selected item property is of the same type as the elements in the source collection, you would bind SelectedItem="{x:Bind CurrentLang, Mode=TwoWay}" without setting SelectedValuePath. – Clemens Commented Mar 17 at 14:39
  • My wish is simple - I get a set of localizations (Locale as a key, Native is just some additional information) and a value (Locale) stored in configuration file. So I want to fill Combo and select an appropriate item by stored Locale without Native knowing. Currently I'm forced to find manually the index by Locale string and then set it through SelectedIndex :( – CyberDemon Commented Mar 19 at 5:57
Add a comment  | 

1 Answer 1

Reset to default 0

Finally I've found the solution.

Changes in IDL file:

  1. runtimeclass LanguageOpt must be declared with BindableAttribute
    [Windows.UI.Xaml.Data.Bindable]
    runtimeclass LanguageOpt
    {
        LanguageOpt();
        String Native {get;};
        String Locale {get;};
    };
  1. LanguageOpt CurrentLang must be changed to
    IInspectable CurrentLang;

I hope this will help to anybody who will encounter the same problem :)

发布评论

评论列表(0)

  1. 暂无评论