wpf - 多个用户控件共享集合依赖属性

标签 wpf dependency-properties itemscontrol

我已经基于列表框实现了我自己的用户控件。它有一个集合类型的依赖属性。当我在一个窗口中只有一个 usercontrol 实例时它工作正常,但是如果我有多个实例,我会遇到它们共享集合依赖属性的问题。下面是一个示例,说明了这一点。

我的用户控件称为 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>

后面的代码:
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>()));
    }
}

我这样使用:
   <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>

我希望以下内容显示在我的窗口中:
First
123
Second
456

但我看到的是:
First
123456
Second
123456

如何让多个 SimpleList 不共享他们的 Numbers Collection ???

最佳答案

找到答案,构造函数需要初始化属性而不是让静态属性自己做:

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

   InitializeComponent();
}

Collection-Type Dependency Properties

关于wpf - 多个用户控件共享集合依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214839/

相关文章:

wpf - 从 Windows 窗体加载 WPF 窗口时询问 System.ExecutionEngineException

wpf - WPF Wrap 面板以何种方式较慢,我们需要虚拟 wrap 面板

wpf - 如何实现内部内容依赖属性?

c# - 如何向 ItemsControl 中的 WPF 按钮添加相同的命令

wpf - 将 ItemsControl 与可拖动项目组合 - Element.parent 始终为 null

c# - 当窗口打开时,如何将数据模板化的文本框集中在窗口中 I​​temsControl 的第一个元素中? (C#, WPF)

javascript - 使用 WPF 处理来自 WebBrowser 控件中托管的 JavaScript 的事件

wpf - 将 DataGrid 行中的 DoubleClick 命令绑定(bind)到 VM

wpf - 来自已完成线程的对象上的 InvalidOperationException

c# - 为什么 "propdp"代码片段不使用 nameof 运算符作为注册属性的名称?