【发布时间】:2019-09-21 19:48:57
【问题描述】:
我有一个项目集合,比如说:
class Myclass : INotifyPropertyChanged
{
public bool Selected { get; set; }
public string ChosenValue { get; set; }
}
然后我有一个此类的可观察集合:
public ObservableCollection<Myclass> _myObColl
最后我有一个绑定到_myObColl 的列表框和一个绑定到另一个集合的单独组合框。
我想更新组合框以更新 _myObColl 列表中所有项目的 ChosenValue 属性。但我不知道怎么做。
组合框选定项绑定到视图模型中称为 currselection 的属性。我想要做的是将 Myclass 的 ChosenValue 属性绑定到 currselection 值。但是我该怎么做呢?也许我不应该考虑绑定,但我想不出另一种方法来更新 items ChosenValue 属性。我尝试了组合的SelectionChanged 事件来循环通过_myObColl。除非在更改组合框后将项目标记为选中,否则此方法有效。
<ComboBox ItemsSource="{Binding Path=DataContext.lstComboList , ElementName=PS4}" SelectedItem="{Binding Path=currselection, Mode=TwoWay}" Margin="10,10,10,10" Width="100"/>
<ListBox ItemsSource="{Binding _myObColl}" Margin="10,10,0,0" >
<ListBox.ItemTemplate x:Uid="asdasd" >
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column ="3" Width="50" VerticalAlignment="Center" Margin="10" IsChecked="{Binding Path=PropA, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
【问题讨论】:
标签: c# wpf xaml data-binding