簡體   English   中英

"ListView 未更新 itemsource ObservableCollection 項目屬性更改"

[英]ListView not updating on the itemsource ObservableCollection Item Property change

考慮以下示例代碼:

視圖.xml

<Grid>
    <ListView Name="NameList" HorizontalAlignment="Left" Height="142" Margin="55,45,0,0" VerticalAlignment="Top" Width="389">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding FirstName}"/>
                    <Label Content="{Binding LastName}" Grid.Column="1"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Content="Button" HorizontalAlignment="Left" Margin="120,256,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

</Grid>

更改項目時,只有在添加,刪除或移動項目時才會觸發ObservableCollection事件。 您必須在項類中實現INotifyPropertyChanged接口,並在集合中注冊每個項事件。

看到這里

如果您對集合進行了大量更改,則可以提高

NotifyCollectionChangedAction.Reset

事件重置所有

看到這里

我也在 WinUI 上使用ListView<\/code>和ObservableCollection<\/code>解決同樣的問題。

你需要了解的是ObservableCollection<\/code>只負責通知集合的變化。 項目級別更改通過INotifyPropertyChanged<\/code>處理。 但是,對於要應用的那些,您需要確保您的綁定模式至少是一種方式,如果需要,可以使用兩種方式。

您的 XAML 應如下所示: Text="{x:Bind Title, Mode=OneWay}<\/code>

不要嘗試那里提出的建議,這是錯誤的: ObservableCollection 沒有注意到其中的項目何時更改(即使使用 INotifyPropertyChanged)<\/a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM