Created
February 12, 2016 10:31
-
-
Save devlights/3b68aad625cca024fb36 to your computer and use it in GitHub Desktop.
[WPF] DependencyPropertyのデフォルト値について (値の共有, do not share the default value)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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