简体   繁体   中英

C# WPF - ComboBox, Values change but SelectedItem not

I know there are a lot questions about the SelectedItem and the ComboBox out there but it seems that they do not solve my problem.

I have a ComboBox where I bind a ObservableCollection of View Models and a TextBox which binds to the Description of the singe View models. I want to change the entries here... Everything works fine when i use the text box, the items will be changed in the ComboBox (even the selected one), but when i change the text in the code behind only the ComboBox List will be updated but not the current selected Value/Item.

My ComboBox looks like this at the moment:

<ComboBox ItemsSource="{Binding Sports, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding SelectedSport, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" Width="365" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="30" Margin="5 5 5 5">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The used ViewModel looks like this:

public class SportViewModel : ViewModelBase
{
    private ObservableCollection<DisciplineViewModel> _disciplinesProperty;

    public Sport Model
    {
        get;
        set;
    }

    public string Description
    {
        get { return Model.Description; }
        set
        {
            if (Model.Description != value)
            {
               Model.Description = value;
               RaisePropertyChanged(() => Description);
            }
        }
    }

    public ObservableCollection<DisciplineViewModel> Disciplines
    {
        get { return _disciplinesProperty; }
        set
        {
            if (_disciplinesProperty != value)
            {
                _disciplinesProperty = value;
                RaisePropertyChanged(() => Disciplines);
            }
        }
    }

    public SportViewModel(Sport source)
    {
       Model = source;
       Disciplines = new ObservableCollection<DisciplineViewModel>();
       foreach(Discipline d in Model.Disciplines)
       {
           Disciplines.Add(new DisciplineViewModel(d, this));
       }
   }

   public override bool Equals(object obj)
   {
       if (obj == null || !(obj is SportViewModel))
           return false;

      return ((SportViewModel)obj).Description == this.Description;
   }

   public override int GetHashCode()
   {
      return base.GetHashCode();
   }
}

and is used here:

class EditSportsDialogViewModel : ViewModelBase
{
  ...
  public ObservableCollection<SportViewModel> Sports 
  {
      get { return _sportsProperty; }
      set
      {
          if (_sportsProperty != value)
          {
              _sportsProperty = value;
          }
      } 
  }

  public SportViewModel SelectedSport
  {
      get { return _selectedSportPorperty; }
      set
      {
          if (_selectedSportPorperty != value)
          {
              if (value != null)
              {
                  Disciplines = value.Disciplines;
              }
              _selectedSportPorperty = value;
              RaisePropertyChanged(() => SelectedSport);
          }
      }
  }

  private void SportRevertExecuted()
  {
      if (SelectedSport != null)
      {
          _context.Entry(SelectedSport.Model).Reload();
      }
      RaisePropertyChanged(() => SelectedSport.Description);
      RaisePropertyChanged(() => SelectedSport);
      RaisePropertyChanged(() => Sports);
      RaisePropertyChanged();
  }
}

The describtion is also changed by the one TextBox which works just fine

<TextBox Text="{Binding SelectedSport.Description, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" TextAlignment="Center" Margin="5" Width="410"/>

But when I call SportRevertExecuted() only the item in the list of the ComboBox will be changed but not the "Text" of the currently selected Item. Maybe someone can help me here, I've tried a lot until now, nothing seems to help.

Ok problem solved. Was quite stupid RaisePropertyChanged(() => SelectedSport.Description); doesn't work as i thought. I've introduced a Method UpdateView() in SportViewModel which raises the PropertyChanged Event in the right Object, now everything works just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM