DataContext:更新 comboBox ItemSsource
DataContext: Updating comboBox's ItemSource
我希望能够更新我的 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 中更新它。
我绑定的 class 和属性是:
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[] { };
}
}
您只需在 class 位置实现接口 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 的其他属性。
我希望能够更新我的 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 中更新它。
我绑定的 class 和属性是:
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[] { };
}
}
您只需在 class 位置实现接口 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 的其他属性。