samc
Topic Author
Posts: 8
Joined: 21 Aug 2019, 19:22

ComboBox selection not updating when model changes

28 May 2020, 23:26

I'm trying to get a combobox to work. It seems like it's mostly there: the combo box appears, it shows the correct list of items, and making a change to the combo box updates my data model. However, when I change my datamodel externally from the combo box (in the example below, I explicitly set the TeamId on the player from code), it is not updating the value shown in the combo box.

What I'm trying to do is display a list of players, and next to each player a combobox with a list of possible teams.

My outer xaml looks like this:
<ItemsControl ItemTemplate="{StaticResource Player_Template}" ItemsSource="{Binding Players}">
</ItemsControl>
The Player_Template looks like this:
<DataTemplate x:Key="Player_Template">
		<StackPanel Orientation="Horizontal">
			<TextBlock Text="{Binding Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#FF87E9FF" Margin="8,0,0,0" TextAlignment="Left" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
			<ComboBox x:Name="TeamSelector" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,0,8,0"
                                ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.Teams}"
                                SelectedValue="{Binding TeamId, Mode=TwoWay}"
								SelectedValuePath="Id"
                                ItemTemplate="{StaticResource TeamOptionTemplate}"/>
		</StackPanel>
	</DataTemplate>
And here is what my data model looks like:

Player:
    public class Player : NotifyPropertyChangedBase
    {
        public string _name;
        public string Name
        {
            get { return _name; }
            set { SetStringProperty(ref _name, value); }
        }

        public int _teamId;
        public int TeamId
        {
            get { return _teamId; }
            set { SetIntProperty(ref _teamId, value); }
        }
    }
Team:
    public class Team : NotifyPropertyChangedBase
    {
        public string _name;
        public string Name
        {
            get { return _name; }
            set { SetStringProperty(ref _name, value); }
        }
        public int _id;
        public int Id
        {
            get { return _id; }
            set { SetIntProperty(ref _id, value); }
        }
    }
And the containing model:
    public class TeamModel : NotifyPropertyChangedBase
    {
        public ObservableCollection<Team> Teams { get; set; }
        public ObservableCollection<Player> Players { get; set; }
    }
I'm a xaml noob, so I'm not sure what I'm doing wrong :(. Any help appreciated.

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: ComboBox selection not updating when model changes

29 May 2020, 01:18

Nothing wrong on your code, this is a bug, I was able to reproduce it.
Could you please report it and we will fix it as soon as possible?

Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: ComboBox selection not updating when model changes

29 May 2020, 18:40

Created ticket #1708 to track this issue.
 
nikobarli
Posts: 178
Joined: 26 Apr 2017, 06:23

Re: ComboBox selection not updating when model changes

08 Jun 2020, 07:58

Does this bug also affect version 2.2.* ?
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: ComboBox selection not updating when model changes

08 Jun 2020, 12:17

Yes, this bug was also present in 2.2 version.
 
User avatar
ai_enabled
Posts: 221
Joined: 18 Jul 2013, 05:28
Contact:

Re: ComboBox selection not updating when model changes

14 Jul 2020, 02:40

Interestingly, the fix doesn't work completely for me.
Everything works properly except the selected item (through the value binding) is not highlighted in the ComboBox dropdown menu.
If I bind the SelectedItemProperty property directly, it works fine.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: ComboBox selection not updating when model changes

14 Jul 2020, 19:16

Could you please open a new ticket about it, I'm not able to reproduce that, whenever I change the bound SelectedValue and open the dropdown menu, highlighted item is correct.

Who is online

Users browsing this forum: Google [Bot] and 1 guest