【问题标题】:Multiple user controls share collection dependency property多个用户控件共享集合依赖属性
【发布时间】:2010-07-09 16:54:40
【问题描述】:

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

我的用户控件名为 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 集合???

【问题讨论】:

    标签: wpf dependency-properties itemscontrol


    【解决方案1】:

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

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

    Collection-Type Dependency Properties

    【讨论】:

    • 我不敢相信。我浪费了几个小时没有找到任何答案。我已经拔掉了我所有的头发。谢谢 :) 虽然我认为这是依赖机制中的一个错误。
    • 这仍然是一个问题,但由于这个问题/答案,解决方案很轻松......
    • 归结为默认值和引用类型 DP 的工作方式:msdn.microsoft.com/en-us/library/aa970563%28v=vs.100%29.aspx
    • 最好改用SetCurrentValue,这样属性仍然可以从Style设置。
    • 这是一个更受欢迎的问题!真是可耻!我花了将近 5 个小时试图弄清楚为什么我的控件不能正常运行.....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多