usercontrol的更改属性不显示更改

时间:2014-08-14 14:27:09

标签: data-binding winrt-xaml win-universal-app

我有一个通用(Windows)应用,并拥有FlipView控件http://msdn.microsoft.com/nl-nl/library/windows/apps/hh850405.aspx

在我的MainPage方法中,我调用了一个填充我的FlipView的方法:

foreach(var s in oc)
{
    var bla = new ProductSet(s.Product1, s.Product2, s.Product3);
    flipView.Items.Add(bla);
}

我也订阅了FlipView_SelectionChanged一段时间后添加更多项目以尝试获得这种延迟加载效果。

我的ProductSet类是usercontrol(ProductSet.xaml)

public sealed partial class ProductSet: UserControl, INotifyPropertyChanged
{
    public Product Product1 { get; set; }
    public Product Product2 { get; set; }
    public Product Product3 { get; set; }

    public ProductSet()
    {
        this.InitializeComponent();
    }

    public ProductSet(Product p1, Product p2, Product p3)
    {
        Product1 = p1;
        Product2 = p2;
        Product3 = p3;

        DataContext = this;
        this.InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

当我更改当前的flipview项目Product1时,它不会显示更改...

Product selectedProduct = (Product)gv.SelectedItem;
ProductSet ps = (NikkiSet)flipView.SelectedItem;
ps.Product1 = selectedProduct; // other object of class Product

我的usercontrol有一个我想要更改的图像:

<Image Stretch="Fill" Source="{Binding Product1.Picture}" Name="imgProduct1" />

但它没有改变。我错过了什么?

1 个答案:

答案 0 :(得分:1)

尝试,

您需要在视图模型中添加一些更改,以便在值更改后重新绑定该属性。使用以下代码。

public sealed partial class ProductSet : UserControl, INotifyPropertyChanged
{
    private Product product1;
    private Product product2;
    private Product product3;

    public Product Product1
    {
        get
        {
            return product1;
        }
        set
        {
            product1 = value;
            OnPropertyChanged("Product1");
        }
    }
    public Product Product2
    {
        get
        {
            return product2;
        }
        set
        {
            product2 = value;
            OnPropertyChanged("Product2");
        }
    }
    public Product Product3
    {
        get
        {
            return product3;
        }
        set
        {
            product3 = value;
            OnPropertyChanged("Product3");
        }
    }

    public ProductSet()
    {
        this.InitializeComponent();
    }

    public ProductSet(Product p1, Product p2, Product p3)
    {
        Product1 = p1;
        Product2 = p2;
        Product3 = p3;

        DataContext = this;
        this.InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

希望这会对你有所帮助。