14

I have implemented my own usercontrol based on listboxes. It has a dependency property with type of a collection. It works fine when I have only one instance of the usercontrol in a window, but if I have multiple instances I get problem that they share the collection dependency property. Below is a sample illustrating this.

My user control called SimpleList:

<UserControl x:Class="ItemsTest.SimpleList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="_simpleList">
    <StackPanel>
        <TextBlock Text="{Binding Path=Title, ElementName=_simpleList}" />
        <ListBox 
            ItemsSource="{Binding Path=Numbers, ElementName=_simpleList}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </StackPanel>    
</UserControl>

Code behind:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace ItemsTest
{
    public partial class SimpleList : UserControl
    {
        public SimpleList()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(SimpleList), new UIPropertyMetadata(""));


        public List<int> Numbers 
        {
            get { return (List<int> )GetValue(NumbersProperty); }
            set { SetValue(NumbersProperty, value); }
        }

        public static readonly DependencyProperty NumbersProperty =
            DependencyProperty.Register("Numbers ", typeof(List<int>), typeof(SimpleList), new UIPropertyMetadata(new List<int>()));
    }
}

I use like this:

   <StackPanel>
        <ItemsTest:SimpleList Title="First">
            <ItemsTest:SimpleList.Numbers>
                <sys:Int32>1</sys:Int32>
                <sys:Int32>2</sys:Int32>
                <sys:Int32>3</sys:Int32>
            </ItemsTest:SimpleList.Numbers>
        </ItemsTest:SimpleList>
        <ItemsTest:SimpleList Title="Second">
            <ItemsTest:SimpleList.Numbers>
                <sys:Int32>4</sys:Int32>
                <sys:Int32>5</sys:Int32>
                <sys:Int32>6</sys:Int32>
            </ItemsTest:SimpleList.Numbers>
        </ItemsTest:SimpleList>
    </StackPanel>

I expect the following to show up in my window:

First
123
Second
456

But what I see is:

First
123456
Second
123456

How do I get multiple SimpleList not to share their Numbers Collection???

Wallstreet Programmer
  • 9,567
  • 3
  • 37
  • 52

1 Answers1

24

Found the answer, the constructor needs to initialize the property instead of letting the static property do itself:

public SimpleList()
{
   SetValue(NumbersProperty, new List<int>()); 

   InitializeComponent();
}

Collection-Type Dependency Properties

Wallstreet Programmer
  • 9,567
  • 3
  • 37
  • 52
  • 1
    I can't believe it. I've wasted several hours without finding any answer. I've pulled all my hair. Thanks :) Although I believe it's a bug in the dependency mechanism. – Chen Oct 29 '15 at 01:40
  • This is still an issue, but thanks to this question/answer, the solution was painless... – PScr May 28 '16 at 23:14
  • It comes down to how default values and reference type DPs work: https://msdn.microsoft.com/en-us/library/aa970563%28v=vs.100%29.aspx – PScr May 30 '16 at 12:08
  • It would be better to use `SetCurrentValue` instead, so that the property is still settable from `Style`. – Grx70 May 25 '18 at 08:27
  • This HAS to be more popular issue! It's scandalous! I spend nearly 5 hours trying to figure out WHY my control wasn't behaving properly..... – GeorgiG Jul 16 '20 at 09:09