亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何更新 xaml 中沒有綁定控件的集合項屬性?

如何更新 xaml 中沒有綁定控件的集合項屬性?

C#
函數式編程 2023-12-17 16:48:52
我有一系列物品,例如: 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;

    }

}


查看完整回答
反對 回復 2023-12-17
  • 1 回答
  • 0 關注
  • 165 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號