Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Problem with UserControl [modified]

Problem with UserControl [modified]

Scheduled Pinned Locked Moved WPF
tutorialcsharpwpfdesignhelp
3 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koleraba
    wrote on last edited by
    #1

    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

    I 1 Reply Last reply
    0
    • K koleraba

      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

      I Offline
      I Offline
      Insincere Dave
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • I Insincere Dave

        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

        K Offline
        K Offline
        koleraba
        wrote on last edited by
        #3

        Great. That solved my problem. Thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups