简体   繁体   English

C# CommunityToolkit.Mvvm ObservableProperty 列表

[英]C# CommunityToolkit.Mvvm ObservableProperty on a list

I want to try the Community Toolkit 8.0 the MVVM part.我想试试Community Toolkit 8.0的 MVVM 部分。

I have a very simple application:我有一个非常简单的应用程序:

.... 
       d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel}"
.... 
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="12*" />
        </Grid.RowDefinitions>
        <Button x:Name="ButtonDirectory" Grid.Row="0" Grid.Column="0" Margin="50 2 0 2" Width="200" Content="List Directory" Command="{Binding ClickButtonListDirectoryCommand, Mode=OneWay}" />
        <ListView x:Name="ListViewDirectories" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding DirectoriesNames}"  />
    </Grid>

and this is my viewmodel:这是我的视图模型:

public partial class MainWindowViewModel : ObservableObject
{
    [ObservableProperty]
    List<string> _directoriesNames = new();

    [RelayCommand]
    private void ClickButtonListDirectory()
    {
        this.DirectoriesNames.Add("Hello");
    }
}

This construct works, when I use string instead of a list - but obviously I want to use something like a list when my view component is a ListView.当我使用string而不是列表时,此构造有效 - 但显然,当我的视图组件是 ListView 时,我想使用类似列表的东西。

The directoriesNames itself gets updated (saw that in the debugger) but it's not displayed in the view ( ListView ). directoriesNames本身已更新(在调试器中看到),但未显示在视图中( ListView )。

Can someone help me out with that?有人可以帮我解决这个问题吗?

Thanks.谢谢。

You need to use ObservableCollection :-您需要使用ObservableCollection :-

ObservableCollection<string> DirectoriesNames = new();

Change your List to ObservableCollection .将您的List更改为ObservableCollection

This code:这段代码:

[ObservableProperty]
List<string> _directoriesNames = new();

Will notify the UI when the DirectoriesNames itself is changed but the only change you are making to DirectoriesNames is instantiating it.DirectoriesNames本身发生更改时将通知 UI,但您对DirectoriesNames所做的唯一更改是对其进行实例化。

You can use a ObservableCollection to notify the UI when you add or remove items.您可以使用ObservableCollection添加删除项目时通知 UI。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 CommunityToolkit.Mvvm 在 ObservableProperty 更改时调用方法 - Call method when ObservableProperty changes using CommunityToolkit.Mvvm 如何在 C# 中使用 Community - How do I bind an object that is periodically modified to a treeview in C# WPF using the CommunityToolkit.MVVM? 如何使用 CommunityToolkit.Mvvm 调用事件 - How to call events using CommunityToolkit.Mvvm 使用 CommunityToolkit.Mvvm 处理可观察对象属性 - Handling observable object properties with CommunityToolkit.Mvvm CommunityToolkit.MVVM 命名空间“命名空间”已包含“类型”的定义 - CommunityToolkit.MVVM The namespace 'namespace' already contains a definition for 'type' 使用 CommunityToolkit.Mvvm 通知 CanExecuteChanged 的 RelayCommand 时出现 StackOverflow 异常 - StackOverflow Exception when notifying RelayCommand of CanExecuteChanged using CommunityToolkit.Mvvm C# 社区工具包 Mvvm 源代码生成器:ObservableProperty - C# Community Toolkit Mvvm Source Generator: ObservableProperty 如何将 CommunityToolkit.Mvvm 中的源代码生成器用于 .NET 框架 4.7.2 WPF 应用程序 - How to use the source generators from CommunityToolkit.Mvvm for a .NET Framework 4.7.2 WPF Application 显示 ObservableGroupedCollection 的正确方法<string, telement>使用 Wpf .NET 6 和 CommunityToolkit.Mvvm 包</string,> - Proper way of displaying an ObservableGroupedCollection<string, TElement> using Wpf .NET 6 and the CommunityToolkit.Mvvm Package 无法在使用 CommunityToolkit.Mvvm 的视图模型中使用 ICommand 属性 - Can't use ICommand attribute in view model using CommunityToolkit.Mvvm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM