Problem with UserControl [modified]
-
Hi everybody I have a problem, for which, I don't seem to find the answer I Created a User Control, in wpf. In that control I defined a Dependency Property as(all the smaller than signs are replaced with '[', and grater than signs are replaced with ']'): public static DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollections[SomeClass]), typeof(MyUserControl), new PropertyMetadata(new ObservableCollection[SomeClass]); I also implemented the standard Getter / Setter for that property as public ObservableCollection[SomeClass] Items { get{return (ObservableCollection[SomeClass])GetValue(ItemsProperty)}; set{SetValue(ItemsProperty, value);} } In the constructor of the user control I subscribe to the CollectionChanged event as: public MyUserControl() { InitializeComponent(); Item.CollectionChanged += new NotifyCollectionChangedEventHandler(items_CollectionChanged); } and in the event handler I have the code that does what is needed private void items_CollectionCHanged(object sender, NotifyCollectionChangedEventArgs e) { //Build control foreach item in e.NewItems and add them to UI and remove from UI controls foreach item in e.OldItems } I add UserControls to my window as follows: [StackPanel] [local:MyUserControl] [local:MyUserControl.Items] [local:SomeClass .... /] [local:SomeClass .... /] [/local:MyUserControl.Items] [/local:MyUserControl] [local:MyUserControl] [local:MyUserControl.Items] [local:SomeClass .... /] [local:SomeClass .... /] [local:SomeClass .... /] [local:MyUserControl.Items] [local:MyUserControl] [/StackPanel] From the start everything worked fine, but if I add two or more instances of MyUserControl, to the window, the first instance is OK, but the other one contains also the items that was defined in the first one. For the example above one of the UserControls whould have 2 instances of control that is build in the CollectionChanged event handler, and the other one would have 5 of them. Does anybody know how to solve this problem. Any advice will be appriciated! Uroš
modified on Monday, April 14, 2008 9:33 PM
-
Hi everybody I have a problem, for which, I don't seem to find the answer I Created a User Control, in wpf. In that control I defined a Dependency Property as(all the smaller than signs are replaced with '[', and grater than signs are replaced with ']'): public static DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollections[SomeClass]), typeof(MyUserControl), new PropertyMetadata(new ObservableCollection[SomeClass]); I also implemented the standard Getter / Setter for that property as public ObservableCollection[SomeClass] Items { get{return (ObservableCollection[SomeClass])GetValue(ItemsProperty)}; set{SetValue(ItemsProperty, value);} } In the constructor of the user control I subscribe to the CollectionChanged event as: public MyUserControl() { InitializeComponent(); Item.CollectionChanged += new NotifyCollectionChangedEventHandler(items_CollectionChanged); } and in the event handler I have the code that does what is needed private void items_CollectionCHanged(object sender, NotifyCollectionChangedEventArgs e) { //Build control foreach item in e.NewItems and add them to UI and remove from UI controls foreach item in e.OldItems } I add UserControls to my window as follows: [StackPanel] [local:MyUserControl] [local:MyUserControl.Items] [local:SomeClass .... /] [local:SomeClass .... /] [/local:MyUserControl.Items] [/local:MyUserControl] [local:MyUserControl] [local:MyUserControl.Items] [local:SomeClass .... /] [local:SomeClass .... /] [local:SomeClass .... /] [local:MyUserControl.Items] [local:MyUserControl] [/StackPanel] From the start everything worked fine, but if I add two or more instances of MyUserControl, to the window, the first instance is OK, but the other one contains also the items that was defined in the first one. For the example above one of the UserControls whould have 2 instances of control that is build in the CollectionChanged event handler, and the other one would have 5 of them. Does anybody know how to solve this problem. Any advice will be appriciated! Uroš
modified on Monday, April 14, 2008 9:33 PM
The problem is that the dependency property is static, therefore the default value only gets initialized once and the same collection is used for all the controls. In the dependency property use the default null and create the collection in the constructor. See http://msdn2.microsoft.com/en-us/library/aa970563.aspx
-
The problem is that the dependency property is static, therefore the default value only gets initialized once and the same collection is used for all the controls. In the dependency property use the default null and create the collection in the constructor. See http://msdn2.microsoft.com/en-us/library/aa970563.aspx