我有一系列物品,例如: class Myclass : INotifyPropertyChanged { public bool Selected { get; set; } public string ChosenValue { get; set; } }然后我就有了這個類的可觀察集合: public ObservableCollection<Myclass> _myObColl最后,我有一個綁定到 _myObColl 的列表框和一個綁定到另一個集合的單獨組合框。我想要更新組合框以更新 _myObColl 列表中所有項目的 ChosenValue 屬性。但我不知道怎么做。組合框選定的項目綁定到視圖模型中名為 currselection 的屬性。我想要做的是將 Myclass 的 ChosenValue 屬性綁定到 currselection 值。但我該怎么做呢?也許我不應該考慮綁定,但我想不出另一種方法來更新項目的 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>
1 回答

瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
如果您希望組合框更改列表框中所選項目的值,您可以像這樣綁定 SelectedItem。
<ComboBox ItemsSource="{Binding ComboList}" SelectedItem="{Binding ElementName=listBox, Path=SelectedItem.ChosenValue}" />
否則,如果您確實想在組合框更改時更改列表中所有項目的屬性,則需要在屬性設置器中的代碼后面執行此操作。
<ComboBox ItemsSource="{Binding ComboList}" SelectedItem="{Binding SelectedValue}"/>
在視圖模型中
private string _SelectedValue;
public string SelectedValue
{
get => _SelectedValue;
set
{
_SelectedValue = value;
foreach (var item in MyObColl.ToList()) item.ChosenValue = _SelectedValue;
}
}
- 1 回答
- 0 關注
- 165 瀏覽
添加回答
舉報
0/150
提交
取消