OnPropertyChanged不更新视图

时间:2016-07-26 10:51:20

标签: wpf xaml data-binding inotifypropertychanged

我有一个ListView,它的itemsource是Elements,它是一个Schranken-Objects列表。 SchrankePresenter是页面的DataContext(在构造函数中设置)。

页面显示正确,直到单击按钮。如果单击一个按钮,则按钮的值名称应该更改,ListView应该有一个项目更多,但没有任何事情发生/更新。

这是SchrankenPresenter:

    public Task DoSomething()
    {
        return Task.Run(() => MyFunction());
    }

    public async Task DoSomethingAsync()
    {
        await Task.Run(() => MyFunction());
    }

Schranke班只有一名成员: 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"); } }

视图如下:

public string Name { get; set; }

PS:如果有人需要DelegateCommand - Implementation:

 <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>

2 个答案:

答案 0 :(得分:1)

如果您想要绑定到项目列表,则应绑定到ObservableCollection

  

表示在何时提供通知的动态数据集合   项目被添加,删除或刷新整个列表。

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

答案 1 :(得分:0)

要从Viewmodel更新用户界面,我们需要提出inotifypropertychanged; observablecollection默认提供inotifypropertychanged实现;我们可以observablecollection参考MVVm pattern

如果您想使用相同的列表,则需要在该特定列表上实施inotifypropertychanged