wpf - Observable Collection 不更新 UI

标签 wpf listview mvvm mvvm-light observablecollection

我正在研究在可观察集合中添加和删除行的能力。
我有 UserControl View ,其中 ListView 绑定(bind)到 ObservableCollection。
我也有删除命令在 ViewModel 中工作,但不更新 UI。

这是我在 ViewModel 上的代码,它继承自 MvvmLight ViewModelBase 类。

 public class ProductInfoViewModel : ViewModelBase
{
    #region Properties

    private ObservableCollection<Product> _productList;
    public ObservableCollection<Product> ProductList
    {
        get { return _productList; }
        set
        {
            _productList =  value;
            RaisePropertyChanged("ProductList");
        }
    }

    #endregion
    #region Constructor
    public ProductInfoViewModel()
    {
        ConnectWebService connect = new ConnectWebService();
        string json = connect.getResponse(@"http://localhost:8082/products");
        ProductList = JsonConvert.DeserializeObject<ObservableCollection<Product>>(json);
        DeleteCommand = new RelayCommand<long>((id) => DeleteCommandHandler(id, ProductList));
    }
    #endregion
    #region Commands
    public ICommand DeleteCommand { get; private set; }
    #endregion
    #region CommandsHandlers
    private void DeleteCommandHandler(long id, ObservableCollection<Product> productList)
    {
        try
        {
            productList.Remove(productList.Where(i => i.ProductId == id).First());

        }
        catch (Exception)
        {
        }
    }

这是我在 XAML 中的代码:
<UserControl.DataContext>
    <local:ProductInfoViewModel/>
</UserControl.DataContext>
<Grid>
    <Grid Margin="0,50,0,100">
        <ListView  Margin="5" SelectionChanged="ListView_SelectionChanged" ItemsSource="{Binding ProductList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="Black" Background="White" BorderBrush="{x:Null}" 
                   x:Name="ProductList">
            <ListView.View>
                <GridView>
                    <GridViewColumn  Header="Kod" Width="50" DisplayMemberBinding="{Binding ProductCode}"/>
                    <GridViewColumn  Header="Nazwa" Width="150" DisplayMemberBinding="{Binding ProductName}" />
                    <GridViewColumn  Header="Typ" Width="150" DisplayMemberBinding="{Binding ProductType}" />
                    <GridViewColumn  Header="Opis" Width="300" DisplayMemberBinding="{Binding ProductDescription}" />
                    <GridViewColumn  Header="Dostępność" Width="150" DisplayMemberBinding="{Binding ProductAvability}" />
                    <GridViewColumn Width="80">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Edytuj" Background="Transparent" BorderThickness="0" Foreground="Blue" Width="50" Margin="0" HorizontalAlignment="Center"
                                        Click="Button_Click"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="80">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Usuń" Background="Transparent" BorderThickness="0" Foreground="Blue" Width="50" Margin="0" HorizontalAlignment="Center" 
                                        Click="Delete_Click" Command="{Binding ProductInfoView.DeleteCommand, Mode=OneWay, Source={StaticResource Locator}}" CommandParameter="{Binding ProductId}"
                                         />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

我的产品类实现了 INotifyPropertyChanged 接口(interface),但我也使用 Fody。

现在,仅当我将 View 更改为另一个 View 然后返回时, View 才会更新。
我不知道出了什么问题。
如果有任何帮助,我将不胜感激!

最佳答案

您正在绑定(bind) Command Button 的属性(property)到您的 Locator 返回的 View 模型资源。您应该将它绑定(bind)到与 ListView 相同的实例。势必会:

<DataTemplate>
    <Button Content="Usuń" Background="Transparent" BorderThickness="0" Foreground="Blue" Width="50" Margin="0" HorizontalAlignment="Center" 
            Click="Delete_Click"
            Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=ListView}}" 
            CommandParameter="{Binding ProductId}"/>
</DataTemplate>

关于wpf - Observable Collection 不更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600337/

相关文章:

wpf - 绑定(bind)到 DataGrid.DataTemplate

silverlight - WP7 WrapPanel & MVVM

c# - 在 WPF 3D 中用圆柱体连接两个球体

c# - 想要将 Cs 文件中的 List 绑定(bind)到 Xaml 中的 Grid,而无需隐藏代码中的任何代码

Python PyGTK ListView 排序

java - 根据Main Activity类的条件在Adapter类的listview中插入图片

c# - 监听业务逻辑的 PropertyChanged 事件

c# - 在 ControlTemplate 的 DataTrigger 中使用时,TemplatedParent 为空

wpf - 本地 WPF C# 编程

WPF XAML ListView 水平线