I know there have been a lot of issues with the .NET 3.5 RadioButton, but I believe this is a different issue that I have not found anything on in my searching. I've put together a very minimal code sample to demonstrate the problem I'm observing.
I have a ListView in my XAML that has its SelectedItem bound to the SelectedModel in my ViewModel. This same XAML contains a StackPanel that shows RadioButton options for the SelectedModel when navigating through items in the ListView. Everything works great
until I add a RadioButton group into the mix. Now, each time I click a record in the ListView that has the third RadioButton IsChecked=true, then if I click a row where the third radio isn't checked, it sets the priorly selected object IsChecked to false.
If I move up and down through the ListView long enough, every RadioButton, and the underlying Boolean values, will be set to false.
Model code:
public class Model { public string Name { get; set; } public bool IsOne { get; set; } public bool IsTwo { get; set; } public bool IsThree { get; set; } }
ViewModel Code:
public class ViewModel: INotifyPropertyChanged { private Model _selectedModel; public event PropertyChangedEventHandler PropertyChanged; public List<Model> ModelList { get; set; } public Model SelectedModel { get { return _selectedModel; } set { _selectedModel = value; OnPropertyChanged("SelectedModel"); } } public ViewModel() { ModelList = new List<Model>(); ModelList.Add(new Model() {Name = "Florida"}); ModelList.Add(new Model() {Name = "Texas"}); ModelList.Add(new Model() {Name = "Arizona"}); ModelList.Add(new Model() {Name = "Washington"}); } public bool IsOne { get {return SelectedModel.IsOne;} set { SelectedModel.IsOne = value; OnPropertyChanged("IsOne"); } } public bool IsTwo { get{return SelectedModel.IsTwo;} set { SelectedModel.IsTwo = value; OnPropertyChanged("IsTwo"); } } public bool IsThree { get { return SelectedModel.IsThree; } set { SelectedModel.IsThree = value; OnPropertyChanged("IsThree"); } } void OnPropertyChanged(string propName) { if (this.PropertyChanged != null) this.PropertyChanged( this, new PropertyChangedEventArgs(propName)); } }
And finally, the XAML code:
<Grid><Grid.ColumnDefinitions><ColumnDefinition Width="100"/><ColumnDefinition /></Grid.ColumnDefinitions><ListView ItemsSource="{Binding Path=ModelList}" SelectedItem = "{Binding SelectedModel}" DisplayMemberPath="Model"><ListView.View><GridView><GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"/></GridView></ListView.View></ListView><StackPanel Grid.Column="1"><RadioButton x:Name="IsOne" Content="One" Margin="3" IsChecked="{Binding Path=SelectedModel.IsOne}" GroupName="{Binding Path=SelectedModel.Name}"/><RadioButton x:Name="IsTwo" Content="Two" Margin="3" IsChecked="{Binding Path=SelectedModel.IsTwo}" GroupName="{Binding Path=SelectedModel.Name}" /><RadioButton x:Name="IsThree" Content="Three" Margin="3" IsChecked="{Binding Path=SelectedModel.IsThree}" GroupName="{Binding Path=SelectedModel.Name}" /></StackPanel></Grid>
Any ideas of what would be causing the binding to behave in the manner?
Thanks,
Glenn
Please mark this post as Answered if it solved your problem, otherwise you can click Vote if it helped you out. -Glenn Thimmes