2016-12-15 94 views
0

我希望能够更新我的Gird中的组合框。我假设我需要某种事件系统。DataContext:更新组合框的ItemSource

我如下束缚:

<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

你只需要实现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和位置的其他属性上实现此接口。

+0

看着INotiftyPropertyChanged并达到我想要的。谢谢。 –