Avalonia
Avalonia copied to clipboard
RadioButton binding is (incorrectly) reset when switching between Tabs or ViewModels
Hi,
WPF shows the same behavior (https://stackoverflow.com/questions/8568861/when-viewmodel-is-changed-to-a-different-instance-a-bound-radiobutton-is-reset-t), but I still think this is a bug.
MainWindow.xaml
<Window.DataTemplates>
<DataTemplate DataType="{x:Type local:TabViewModel}">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<RadioButton IsChecked="{Binding !RadioButtonProperty}" Content="Value: false"/>
<RadioButton IsChecked="{Binding RadioButtonProperty}" Margin="5, 0" Content="Value: true"/>
</StackPanel>
</DataTemplate>
</Window.DataTemplates>
<TabControl>
<TabItem Content="{Binding VM1}" Header="Tab 1"></TabItem>
<TabItem Content="{Binding VM2}" Header="Tab 2"></TabItem>
</TabControl>
MainWindow.xaml.cs
public class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class MainWindowViewModel : ViewModelBase
{
public TabViewModel VM1 { get; set; }
public TabViewModel VM2 { get; set; }
public MainWindowViewModel()
{
VM1 = new TabViewModel();
VM2 = new TabViewModel();
}
}
public class TabViewModel : ViewModelBase
{
bool _RadioButtonProperty;
public bool RadioButtonProperty
{
get
{
return _RadioButtonProperty;
}
set
{
if (_RadioButtonProperty == value)
return;
_RadioButtonProperty = value;
OnPropertyChanged();
}
}
}
Steps to reproduce:
- Run app
- Switch radio button on the first tab to True
- Go to the second tab
- Go back to the first tab
Expected outcome: RadioButton in the first tab is still True Observed outcome: RadioButton is reset to False
Not reproduced with a CheckBox.
I don't require Group names to reproduce, so I doubt https://github.com/AvaloniaUI/Avalonia/issues/2717 or https://github.com/AvaloniaUI/Avalonia/issues/2903 are related.
Avalonia 0.9.12 and 0.10.0 on Windows 10 and Ubuntu 20.04
If this is not a bug, how can I work around it?