I want to be able to update a ComboBox within my Gird. I'm assuming I need some sort of event system.
I've bound it as follows:
<ComboBox Name="ScreenLocations" Grid.Row="1" Margin="0,0,0,175" ItemsSource="{Binding Path=CurrentPlayer.CurrentLocation.CurrentDirections}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path= Location}"/>
my xaml.cs is as follows:
public partial class MainWindow : Window
{
GameSession _gameSession;
public MainWindow()
{
InitializeComponent();
_gameSession = new GameSession();
DataContext = _gameSession;
}
}
I want to be able to change the CurrentDirections
property and to have it updated in the UI.
The class and properties I have it bound to is:
public class Location
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Quest[] AvailableQuests { get; set; }
public Monster[] LocationMonsters { get; set; }
public Location[] CurrentDirections { get; set; }
public Location(string name, string description, Quest[] availableQuests, int id)
{
Name = name;
Description = description;
AvailableQuests = availableQuests;
ID = id;
CurrentDirections = new Location[] { };
LocationMonsters = new Monster[] { };
AvailableQuests = new Quest[] { };
}
}
You just need to implement interface System.ComponentModel.INotifyPropertyChanged on class Location. This will oblige you to define a PropertyChanged event that interested parties (such as the bound ComboBox) can subscribe to in order to detect changes, and you can then reimplement CurrentDirections as follows, such that it notifies interested parties of the change via this event :
private Location[] currentDirections;
public Location[] CurrentDirections
{
get {return currentDirections;}
set {currentDirections = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CurrentDirections"));}
}
For completeness you should consider implementing this interface on Player, and for the other properties of Location.