[英]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. 我知道有很多关于SelectedItem和ComboBox的问题,但似乎它们没有解决我的问题。
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. 我有一个ComboBox,我绑定了View Models的ObservableCollection和一个TextBox,它绑定到单个View模型的描述。 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. 我想在这里更改条目...当我使用文本框时,一切正常,项目将在ComboBox中更改(甚至是选定的一个),但是当我更改代码中的文本后面只有ComboBox列表将要更新但不是当前选定的值/项目。
My ComboBox looks like this at the moment: 我的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>
The used ViewModel looks like this: 使用过的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();
}
}
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也改变了描述,它工作得很好
<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. 但是当我调用SportRevertExecuted()
只会更改ComboBox列表中的项目,但不会更改当前所选项目的“文本”。 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);
是非常愚蠢的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. 我在UpdateView()
引入了一个Method UpdateView()
,它在右边的Object中引发了PropertyChanged事件,现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.