我希望能够在我的Gird中更新ComboBox。我假设我需要某种事件系统。
我把它绑定如下:
<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}"/>
我的xaml.cs如下:
public partial class MainWindow : Window
{
GameSession _gameSession;
public MainWindow()
{
InitializeComponent();
_gameSession = new GameSession();
DataContext = _gameSession;
}
}
我希望能够更改CurrentDirections
属性并在UI中更新。
我所拥有的类和属性是:
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[] { };
}
}
答案 0 :(得分:0)
您只需要在类Location上实现接口System.ComponentModel.INotifyPropertyChanged。这将迫使您定义一个PropertyChanged事件,感兴趣的各方(例如绑定的ComboBox)可以订阅以检测更改,然后您可以按如下方式重新实现CurrentDirections,以便通过此事件通知感兴趣的各方:
private Location[] currentDirections;
public Location[] CurrentDirections
{
get {return currentDirections;}
set {currentDirections = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CurrentDirections"));}
}
为了完整性,您应该考虑在Player上实现此接口,以及Location的其他属性。