-1

I have a ListView which has its itemsource to Elements which is a List of Schranken-Objects. The SchrankePresenter is the DataContext of the page(set in the constructor).

The page is displayed correctly, until a button is clicked. If a button is clicked, the value Name of the button should change and the ListView should have one item more, but nothing happens/updates.

Here is the SchrankenPresenter:

 public class SchrankePresenter : INotifyPropertyChanged
    {
        private List<Schranke> _elements;
        public List<Schranke> Elements
        {
            get { return _elements; }
            set
            {
                _elements = value;
                OnPropertyChanged("Elements");
            }
        }
        public ICommand ClickCommand { get; set; }
    private void OnPropertyChanged(string propName)
    {
        Debug.WriteLine($"on Property changed with {propName}");
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public SchrankePresenter()
    {
        var elements = new List<Schranke>();
        for (var i = 1; i < 15; i++)
            elements.Add(new Schranke() { Name = $"{i}: Schranke" });

        Elements = elements;
        Debug.WriteLine("ctor");
        ClickCommand = new DelegateCommand<Schranke>(ClickAction);
    }

    public void ClickAction(Schranke item)
    {
        VibrationDevice.GetDefault().Vibrate(TimeSpan.FromMilliseconds(150));

        Debug.WriteLine($"{item?.Name} was clicked");
        item.Name = "was clicked";
        OnPropertyChanged("Name");
        Elements.Add(new Schranke() { Name = "addednew element" });
        OnPropertyChanged("Elements");

    }

}

The class Schranke has only one member: public string Name { get; set; }

The view looks like:

 <ListView ItemsSource="{Binding Elements, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" x:Name="lv_schranken" Grid.Row="1" SelectedItem="{Binding  SelectedItem}">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <Button Height="80"
                            HorizontalAlignment="Stretch"
                            Command="{Binding ElementName=lv_schranken, Path=DataContext.ClickCommand}"
                            CommandParameter="{Binding}"
                            Style="{StaticResource TransparentStyle}">
                        <Grid>
                                .
                                .  (some grid stuff)
                                .

                            <TextBlock Name="schranken_name"
                                       Grid.Row="1"
                                       Grid.Column="1"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       FontWeight="ExtraLight"
                                       FontSize="25"
                                       Foreground="Black"
                                       Text="{Binding Name, Mode=TwoWay}" />
                        </Grid>
                    </Button>
                </DataTemplate>

            </ListView.ItemTemplate>

        </ListView>

PS: if some needs the DelegateCommand - Implementation:

 public class DelegateCommand<T> : ICommand
    {
    private Action<T> _clickAction;

    public DelegateCommand(Action<T> clickAction)
    {
        _clickAction = clickAction;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        _clickAction((T)parameter);
    }
}
student96
  • 345
  • 1
  • 4
  • 14

2 Answers2

1

If your want to bind to a List of items, you should bind to an ObservableCollection:

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

https://msdn.microsoft.com/de-de/library/ms668604(v=vs.110).aspx

stefan.s
  • 3,489
  • 2
  • 30
  • 44
0

For updating the UI from Viewmodel we're required to raised inotifypropertychanged; observablecollection provides the inotifypropertychanged implementation by default; we can refer observablecollection for MVVm pattern.

If you want to use list for the same then you need to implement the inotifypropertychanged on that particular list

Community
  • 1
  • 1