Skip to content

Instantly share code, notes, and snippets.

@devlights
Created February 12, 2016 10:31
Show Gist options
  • Select an option

  • Save devlights/3b68aad625cca024fb36 to your computer and use it in GitHub Desktop.

Select an option

Save devlights/3b68aad625cca024fb36 to your computer and use it in GitHub Desktop.
[WPF] DependencyPropertyのデフォルト値について (値の共有, do not share the default value)
// DependencyPropertyは、デフォルト値の指定を宣言時に行うことが
// できるが、デフォルト値は全てのクラスで同じインスタンスが使われる。
// そのため、Listのようにスカラ値ではないオブジェクトの場合は、一つのデフォルト値用
// インスタンスが共有されるため、インスタンスを作成するたびに内部のカウントがどんどん増えていってしまう。
// これを防ぐには、コンストラクタできっちり初期化する必要がある。
// (http://stackoverflow.com/questions/3214839/multiple-user-controls-share-collection-dependency-property)
// (http://blog.okazuki.jp/entry/2014/08/17/220810)
// 例)
SetValue(XXXXProperty, new ObservableCollection<T>());
// 例2)
this.XXXX = new ObservableCollection<T>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment