WPF组合框selectedvalue在更新selecteditem时不更新
存在绑定到combobox的可观察集合WPF组合框selectedvalue在更新selecteditem时不更新,wpf,binding,combobox,selectedvalue,Wpf,Binding,Combobox,Selectedvalue,存在绑定到combobox的可观察集合 public ObservableCollection<AnyType> AnyTemplates { get; set; } 和绑定到此集合的组合框: <ComboBox Name="cmbKeyA" Width="100" SelectedValue="{Binding Path=KeyAName}" ItemsSource="{Binding Path=DataCon
public ObservableCollection<AnyType> AnyTemplates { get; set; }
和绑定到此集合的组合框:
<ComboBox Name="cmbKeyA"
Width="100"
SelectedValue="{Binding Path=KeyAName}"
ItemsSource="{Binding Path=DataContext.KeyTemplates, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Name"
SelectedValuePath="Name"/>
第一个集合为空。然后,当我在集合中添加新值时,选中复选框EdItem change to this value。如果我更改集合项中的Name属性,则组合框selectedItem将被更改,我将看到DisplayMemberPath将更改为新值,但所选值在我再次手动选择此项之前不会更改。
Name属性集合元素调用PropertyChanged事件。
为什么这不起作用
摘要:当我以编程方式更改comboxo SelectedItem中的NameProperty时,combobox SelectedItem会更改,但SelectedValue不会更新,直到我再次在combobox中手动更改它。尝试使用combobox的ItemStyle容器,使其看起来像这样:
<ComboBox Name="cmbKeyA"
Width="100"
SelectedValue="{Binding Path=KeyAName}"
ItemsSource="{Binding AnyTemplates}"
DisplayMemberPath="Name"
SelectedValuePath="Name">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsCurrent, Mode=TwoWay}"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
另外,请确保您已经使用NotifyPropertyChanged完成了所有操作,并设置了DataContext。另一件不应该做的事情是确保您首先在加载时在视图模型中设置初始值,然后只更改SelectedItem