【问题标题】:ObservableCollection didn't updateObservableCollection 没有更新
【发布时间】:2015-12-16 17:56:58
【问题描述】:

我的 ObservableCollection 有问题,我不知道该怎么做。 我与我的 SQl 基础有联系(使用 EF)。

 public class ProductModel 

{
    private static List<ProductDbObject> ProductDbObjects { get; set; }
    public ObservableCollection<ProductDbObject> OProductDbObjects { get; set; }

    public ProductModel()
    {
        CollectionChanged();

    }
    public void CollectionChanged()
    {
        ProductDbObjects = new List<ProductDbObject>();
        ProductDbObjects = Services.productService.GetAll();
        OProductDbObjects = new ObservableCollection<ProductDbObject>(ProductDbObjects);
    }
}

}

我从 base 中获取所有项目并将其保存在列表中并在 ObservableCollection 中列出。

<Window x:Class="NoweChili.View.AdminView.EditProductView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:NoweChili.View.AdminView"
    mc:Ignorable="d"
    Title="Edycja Produktów" Height="600" Width="800">

<Grid Margin="20" >
    <Grid.RowDefinitions>
        <RowDefinition Height="5*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
     <ColumnDefinition Width="5*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListView x:Name ="ProductListListView"  ItemsSource="{Binding OProductDbObjects}" ></ListView>
    <Grid Grid.Row="0" Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="0.1*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="0.3*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="5*"/>
        </Grid.RowDefinitions>
        <Button x:Name="ProductEditButton" Click="ProductEditButton_OnClick" Content="Edytuj"></Button>
        <Button x:Name="ProductDeleteButton" Click="ProductDeleteButton_OnClick" Content="Usuń" Grid.Row="2"></Button>
        <Button x:Name="ProductBackButton"  Content="Cofnij" Grid.Row="4"></Button>
    </Grid>

</Grid>

项目显示正确,但是当我删除其中一个时,observableCollection 没有更新,项目仍然在列表中。

 private void ProductDeleteButton_OnClick(object sender, RoutedEventArgs e)
    {
       ProductDbObject productToDelete = new ProductDbObject();
        productToDelete = (ProductDbObject) ProductListListView.SelectedItem;
        if (productToDelete == null)
        {
            var result = MessageBox.Show("Proszę zaznaczyć obiekt na liście");
        }

        else
        {
            Services.productService.DeleteEntity(productToDelete);
            Services.productService.SaveChange();            
        }

你能帮帮我吗? :)

【问题讨论】:

    标签: c# wpf windows xaml


    【解决方案1】:

    我认为问题在于,尽管您使用该服务删除了产品,但这并没有从您的 ObservableCollection 中删除该产品 - 您需要调用 Remove(productToDelete)

    【讨论】:

    • 你有权利 :) productModel.OProductDbObjects.Remove(productToDelete);解决这个问题:)谢谢
    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多