I have the below UI, which allows you to select a team in the left, then edit properties of the selected team on the right. Here's an example scenario demonstrating the issue:
- Select Dragon team
- Rename to Smaug, press save.
- "Dragon" in selection panel on left doesn't update to "Smaug". However, if I select another team, the reselect "Dragon", the textbox on the right side still (correctly) shows "Smaug". I'm pretty sure this means that the databound collection is correctly being updated.
- Close the Settings window, then reopen it.
- Left panel now (correctly) shows "Smaug".
The list of teams is being stored as an observable collection:
public class TeamList : ObservableCollection<Team>
{
public TeamList() : base() { }
}
Team list on the left is being populated/bound:
SettingsWindow.xaml
<ListView ItemsSource="{Binding}" Grid.Column="0" Grid.Row="1" DisplayMemberPath="name"
SelectionChanged="ListTeamSelected" SelectionMode="Single">
<!--<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Foreground" Value="{Binding color}" />
</Style>
</ListView.ItemContainerStyle>-->
</ListView>
SettingsWindow.xaml.cs
public Team selectedTeam { get; set; }
public SettingsWindow()
{
teams = TeamManager.Instance().teamList;
this.DataContext = this.teams;
if (!Application.Current.Resources.Contains("selectedTeam"))
Application.Current.Resources.Add("selectedTeam", selectedTeam);
InitializeComponent();
}
Data on the right is being populated and saved:
SettingsWindow.xaml.cs
private void ClickSaveData(object sender, RoutedEventArgs e)
{
selectedTeam.name = TeamName.Text;
selectedTeam.color = PrimaryColorPicker.SelectedColor;
selectedTeam.secondaryColor = SecondaryColorPicker.SelectedColor;
saved = true;
}
private void ListTeamSelected(object sender, RoutedEventArgs e)
{
selectedTeam = (Team)(sender as ListView).SelectedItems[0];
TeamInfo.Visibility = Visibility.Visible;
TeamName.Text = selectedTeam.name;
PrimaryColorPicker.SelectedColor = selectedTeam.color;
SecondaryColorPicker.SelectedColor = selectedTeam.secondaryColor;
}
Twofold question:
Am I doing anything wrong with my databinding that's causing this issue? (I'm new at WPF)
If not, is there a way for me to force the UI to update the list on the left? (this seems vaguely hacky to me)
Thank you in advance for any assistance!