【问题标题】:C# WPF - ComboBox, Values change but SelectedItem notC# WPF - 组合框,值改变但 SelectedItem 没有
【发布时间】:2015-02-18 12:41:23
【问题描述】:

我知道有很多关于 SelectedItem 和 ComboBox 的问题,但似乎它们并没有解决我的问题。

我有一个组合框,我在其中绑定了一个视图模型的 ObservableCollection 和一个绑定到单个视图模型的描述的文本框。我想在这里更改条目...当我使用文本框时一切正常,项目将在 ComboBox 中更改(甚至是选定的项目),但是当我更改代码后面的文本时,只有 ComboBox 列表会被更新,但不是当前选定的值/项目。

我的 ComboBox 现在看起来像这样:

<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>

使用的 ViewModel 如下所示:

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();
   }
}

在这里使用:

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();
  }
}

描述也被一个 TextBox 改变了,它工作得很好

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

但是当我调用SportRevertExecuted() 时,只有 ComboBox 列表中的项目会被更改,而不是当前选定项目的“文本”。也许有人可以在这里帮助我,到目前为止我已经尝试了很多,似乎没有任何帮助。

【问题讨论】:

    标签: c# wpf xaml binding combobox


    【解决方案1】:

    好的,问题解决了。很愚蠢RaisePropertyChanged(() =&gt; SelectedSport.Description); 不像我想的那样工作。我在 SportViewModel 中引入了一个方法 UpdateView(),它在正确的对象中引发 PropertyChanged 事件,现在一切正常。

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 2021-10-15
      • 1970-01-01
      • 2018-05-03
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2021-07-27
      相关资源
      最近更新 更多