I'm developing a simple WPF application. Currently, the application only manages a list of video games in a ComboBox. The game list is serialized to/from an XML file.
The feature that is currently broken is the ability to select a game from the ComboBox which makes it the "active" game to be managed. The active game will be stored as an application setting. To achieve this, I'm using two-way databinding and binding the SelectedItem in the ComboBox to the SelectedGame in the ViewModel.
When I run the application and select an item from the ComboBox, I get a null reference exception on the Game.Equals(Game other) method. While debugging, I saw that DependencyProperty.UnsetValue is being passed as the argument to the overriden Game.Equals(object obj) method which causes obj as Game to make obj null.
I don't understand why Equals is being called in the first place when all I'm doing is selecting an item from the ComboBox value, so I'm guessing it's something native to WPF. The code doesn't seem to hit any other breakpoints before heading to the Equals method, so I'm not even sure how to debug the issue. I also don't understand where DependencyProperty.UnsetValue is coming from. I'm just utterly lost here and would appreciate any insight, including how I can debug this further.
EDIT: As Glen alluded to, Equals must be called by some underlying component of WPF. My solution, at least for now, was to simply add a null check to my Equals override.
Model Class
public class Game : IEntity, IEquatable<Game>
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("ExecutablePath")]
public string ExecutablePath { get; set; }
public Game(string name, string executablePath)
{
Name = name;
ExecutablePath = executablePath;
}
private Game() { } // Required for XML serialization.
public bool Equals(Game other)
{
return Name.EqualsIgnoreCase(other.Name);
}
public override bool Equals(object obj)
{
return Equals(obj as Game);
}
}
ViewModel
public class GamesViewModel
{
// The GameRepository retrieves the game collection from the XML file.
private readonly GameRepository _gameRepository;
public ObservableCollection<Game> Games
{
get { return new ObservableCollection<Game>(_gameRepository.Items); }
}
public Game SelectedGame
{
get { return Settings.Default.ActiveGame; }
set
{
if (!Settings.Default.ActiveGame.Equals(value))
{
Settings.Default.ActiveGame = value;
Settings.Default.Save();
}
}
}
public GamesViewModel()
{
_gameRepository = RepositorySingletons.GameRepository;
}
}
View
<UserControl x:Class="ENBOrganizer.UI.Views.GamesView"
xmlns="http://ift.tt/o66D3f"
xmlns:x="http://ift.tt/mPTqtT"
xmlns:mc="http://ift.tt/pzd6Lm"
xmlns:d="http://ift.tt/pHvyf2"
xmlns:local="clr-namespace:ENBOrganizer.UI.ViewModels"
mc:Ignorable="d" >
<UserControl.DataContext>
<local:GamesViewModel />
</UserControl.DataContext>
<Grid>
<ComboBox Name="GamesComboBox" ItemsSource="{Binding Games}" SelectedItem="{Binding SelectedGame}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" Padding="5,0,0,0" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</UserControl>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire