ObservableCollection依赖项属性未在Usercontrol上更新

时间:2014-04-27 18:18:10

标签: c# wpf data-binding

此处可观察的集合正在更新,但它没有反映在UI上。

XAML

<ComboBox Name="ProductsCombo" Width="200" Grid.Column="1" HorizontalAlignment="Left"
                  ItemsSource="{Binding Products}"
                  DisplayMemberPath="Name"
                  SelectedValue="ID"
                  SelectionChanged="OnProductSelected"/>

<Button Click="AddNewProduct" Content="Add New Product" Width="105" Margin="5"/>

在我的代码背后

public partial class ProductsAdd : UserControl
{
    SampleContext DbContext = new MedicalShopContext();


    public ProductsAdd()
    {
        DataContext = this;

        InitializeComponent();

        Products = new ObservableCollection<Product>(DbContext.Products);
        ProductsCombo.SelectedItem = Products[0];
    }

    public void AddNewProduct(string ProductName, string ProductDescription)
    {
        NewProduct = new Product()
        {
            Name = ProductName,
            Description = ProductDescription
        };

        DbContext.Products.Add(NewProduct);
        DbContext.SaveChanges();
        Products.Add(NewProduct);
    }

    public ObservableCollection<Product> Products
    {
        get { return (ObservableCollection<Product>)GetValue(ProductsProperty); }
        set { SetValue(ProductsProperty, value); }
    }



public static readonly DependencyProperty ProductsProperty =
            DependencyProperty.Register("Products",
            typeof(ObservableCollection<Product>),
            typeof(ProductsAdd),
            new PropertyMetadata(null));

    public Product NewProduct
    {
        get { return (Product)GetValue(NewProductProperty); }
        set { SetValue(NewProductProperty, value); }
    }

    // Using a DependencyProperty as the backing store for NewProduct.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NewProductProperty =
        DependencyProperty.Register("NewProduct",
        typeof(Product),
        typeof(ProductsAdd),
        new PropertyMetadata(null));

    private void AddNewProduct(object sender, RoutedEventArgs e)
    {
        NewProductInput productInputWindow = new NewProductInput();
        productInputWindow.Show();
    }
}

如果我们从同一个UI调用,则上述绑定有效。但是从其他窗口调用时它没有更新

以下是点击按钮时显示的新窗口代码

public partial class NewProductInput : Window
{

    public NewProductInput()
    {
        InitializeComponent();
    }

    private void Close(object sender, RoutedEventArgs e)
    {
        ProductsAdd productsAddWindow = new ProductsAdd();
        productsAddWindow.AddNewProduct(txtProductName.Text, txtProductDescription.Text);
        this.Close();            
    }
}

2 个答案:

答案 0 :(得分:1)

问题是,在您的UI控件中,Products集合仅填充一次,然后永远不会更新。

Products = new ObservableCollection<Product>(DbContext.Products);

当你写这个时,它意味着Observable集合将包含来自DbContext.Products的相同项目,但是如果DbContext.Products更改它将不会更新。这就是窗户关闭时会发生什么。我建议你处理Closed事件并以相同的方式再次重新创建集合。

private void AddNewProduct(object sender, RoutedEventArgs e)
{
    NewProductInput productInputWindow = new NewProductInput();
    productInputWindow.Show();
    productInputWindow.Closed += OnProductInputWindowClosed;
}

private void OnProductInputWindowClosed(object sender, RoutedEventArgs e)
{
    (sender as Window).Closed -= OnProductInputWindowClosed;
    Products = new ObservableCollection<Product>(DbContext.Products);
}

这是最简单的解决方案,但不是最好的解决方案,因为这会强制重新创建ComboBox中的所有UI项目。

答案 1 :(得分:0)

看起来您有2个不同的ProductsAdd用户控件实例。以下行创建了一个新的产品添加实例,一个没有任何内容的实例     ItemsSource =“{Binding Products}”

ProductsAdd productsAddWindow = new ProductsAdd();