microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.35k stars 677 forks source link

Binding to RadioButtons.SelectedIndex does not work in WinUI3 desktop. #6335

Closed hayashida-katsutoshi closed 1 year ago

hayashida-katsutoshi commented 2 years ago

Describe the bug

Binding to RadioButtons.SelectedIndex does not work in WinUI3 desktop.

Microsoft.WindowsAppSDK version 1.0.0.

Steps to reproduce the bug

<RadioButtons SelectedIndex="{Binding SelectedTheme}" Margin="0,0,0,24">
    <RadioButtons.Header>
        <TextBlock Text="Theme Mode"  Style="{StaticResource SubtitleTextBlockStyle}"/>
    </RadioButtons.Header>
    <RadioButtons.Items>
        <x:String>Light</x:String>
        <x:String>Dark</x:String>
    </RadioButtons.Items>
</RadioButtons>
[ViewModel]
public partial class SettingsViewModel
{
    [PropertyCallMethod("OnSelectedTheme")]
    [Property]
    int selectedTheme;

    void OnSelectedTheme()
    {
        App.Current.RequestedTheme = selectedTheme == 0 ? Microsoft.UI.Xaml.ApplicationTheme.Light : Microsoft.UI.Xaml.ApplicationTheme.Dark;
    }
}

MvvmGen generates concrete properties as below.

    partial class SettingsViewModel : global::MvvmGen.ViewModels.ViewModelBase
    {
        public SettingsViewModel()
        {
            this.OnInitialize();
        }

        partial void OnInitialize();

        public int SelectedTheme
        {
            get => selectedTheme;
            set
            {
                if (selectedTheme != value)
                {
                    selectedTheme = value;
                    OnPropertyChanged("SelectedTheme");
                    OnSelectedTheme();
                }
            }
        }
    }

It looks similar with #4893, but it does not use ItemsSource.

Expected behavior

The sample code has a radio buttons. It is expected to call setter of SettingsViewModel.SelectedTheme.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 0.8: 0.8.0 (If you're seeing your issue in older previews of WinUI 3, please try this release)

Windows app type

Device form factor

Desktop

Windows version

May 2020 Update (19041)

Additional context

No response

nikolayvpavlov commented 2 years ago

The bug also to happens when setting SelectedItem and SelectedItemIndex from code.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

hayashida-katsutoshi commented 1 year ago

Still waiting for update.

kmahone commented 1 year ago

Thanks for reporting this issue. I took a look and I did not run into issues binding to RadioButtons.SelectedIndex.

There are two issues with the code in the repro steps.

<RadioButtons SelectedIndex="{Binding SelectedTheme}" >

By default, Binding is OneWay. So interacting with the RadioButtons UI will not update the ViewModel. You can enable two way binding with:

<RadioButtons SelectedIndex="{Binding SelectedTheme, Mode=TwoWay}" >

The second issue is with this line:

App.Current.RequestedTheme = selectedTheme == 0 ? Microsoft.UI.Xaml.ApplicationTheme.Light : Microsoft.UI.Xaml.ApplicationTheme.Dark;

Application.RequestedTheme is only settable on app startup. See the Remarks section of the doc here: https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.requestedtheme?view=windows-app-sdk-1.4. So this code will not have the intended effect.

From my testing, RadioButtons.SelectedIndex appears to be working as expected in the general case.