5

I want to try the Community Toolkit 8.0 the MVVM part.

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.

The directoriesNames itself gets updated (saw that in the debugger) but it's not displayed in the view (ListView).

Can someone help me out with that?

Thanks.

3 Answers 3

11

Change your List to ObservableCollection.

This code will notify the UI when the DirectoriesNames itself is changed but the only change you are making to DirectoriesNames is instantiating it.

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

The ObservableCollection notifies the UI when you add or remove items.

[ObservableProperty]
ObservableCollection<string> _directoriesNames = new();
Sign up to request clarification or add additional context in comments.

1 Comment

Yeps, that's it. Thank you very much for the additional explanation why List will not work :-)
6

You need to use ObservableCollection :-

ObservableCollection<string> DirectoriesNames = new();

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Getting answers together: use [ObservableProperty] ObservableCollection

Full sample:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.