简体   繁体   English

UWP用户控件:绑定依赖项属性的集合更改事件

[英]UWP User control: collection changed event of a binded dependency property

When I call Generate() the events associated with the ObservableCollection ( X ) is not fired. 当我调用Generate() ,不会触发与ObservableCollection( X )相关的事件。

What am I doing wrong? 我究竟做错了什么?

The code: 编码:

MyControl.xaml.cs MyControl.xaml.cs

 public ObservableCollection<double> X
    {
        get { return (ObservableCollection<double>)GetValue(XProperty); }
        set { SetValue(XProperty, value); }
    }

    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register(
            "X", typeof(ObservableCollection<double>),
            typeof(MyControl), 
            new PropertyMetadata(
                new ObservableCollection<double>(),
                new PropertyChangedCallback(OnXChanged)));

   private static void OnXChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as MyControl;

            // DoSomething
        }

A XAML random page that uses MyControl: 使用MyControl的XAML随机页面:

 <local:MyControl
            Title="Test"
            X="{Binding TestX, Mode=TwoWay}"
            />

That page's .cs 该页面的.cs

   public sealed partial class MainPage : Page
    {
    public ObservableCollection<double> TestX { get; set; }
    private static Random rand_ = new Random();

    public MainPage()
    {
        this.InitializeComponent();
        TestX = new ObservableCollection<double>();
    }

    private void Generate()
        {
            TestX.Clear();

            for (int i = 0; i < 100; ++i)
            {
                TestX.Add(rand_.Next(1, 100));
            }
        }
   ....
 }

Please note that I don't see any BindingExpression error in the output window. 请注意,我在输出窗口中没有看到任何BindingExpression错误。

Update 更新

I noticed that if do like this in the page, it works: 我注意到,如果在页面中这样做,它会起作用:

TestX = new ObservableCollection<double>();
this.MyUserControlInstance.X = TestX;

You're missing two things there: 您在这里缺少两件事:

First: 第一:
Make sure you set the DataContext in your constructor: 确保在构造函数中设置DataContext

public MainPage()
{
    this.InitializeComponent();

    DataContext = this; // Important, but you should use a seperated ViewModel instead

    TestX = new ObservableCollection<double>();
} 

Second: 第二:
Your class is missing the INotifyPropertyChanged implementation, as well as the PropertyChanged call for your TestX property: 您的类缺少INotifyPropertyChanged实现,以及TestX属性的PropertyChanged调用:

private ObservableCollection<double> _testX;
public ObservableCollection<double> TestX
{
    get { return _testX; }
    set
    {
        if (value == _testX) return;
        _testX = value;
        OnPropertyChanged();
    }
}

private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null)
        handler(this, new PropertyChangedEventArgs(propertyName));
}

Side note: Do MVVM! 旁注:做MVVM!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM