0

当我尝试像这样将附加的属性集合添加到面板时;

<Grid x:Name="MainContent" Grid.Row="1">
    <StackPanel x:Name="MainContentSp">

        <do:AttachCollection.Col>
            <do:AttachItem x="y"/>
            <do:AttachItem x="z"/>
        </do:AttachCollection.Col>

        <TextBlock x:Name="tb1" Text="xx"/>
        <TextBlock x:Name="tb2" Text="yy"/>

    </StackPanel>
</Grid>

而是将其分配给网格...如何将其附加到面板?

4

1 回答 1

0

事实证明,附加属性与每个 ui 元素共享一个列表实例,因此我不得不使用一种相当老套的方式为每个元素提供它自己的列表实例。

public static readonly
    DependencyProperty
    XProperty =
        DependencyProperty
            .RegisterAttached
            ("X",
                typeof(List<X>),
                typeof(X),
                new PropertyMetadata(new List<X>()));

public static readonly
    DependencyProperty
    XProperty =
        DependencyProperty
            .RegisterAttached
-->         ("XInternal",
                typeof(List<X>),
                typeof(X));
-->(removed)

现在使用 getter 为每个 ui 元素创建一个新的列表实例:

public static List<X> GetX(UIElement element)
{
    var list = ((List<X>)(element.GetValue(XProperty)));
    if (list == null)
    {
        list = new List<X>();
        SetX(element, list);
    }
    return list;
}
于 2013-03-27T13:16:17.917 回答