0

我有一个以列表为成员的派生面板。如何从 xaml 绑定到列表?

class mypanel : Panel
{
    IList<int> mylist;
    ...
}

编辑:

public static DependencyProperty myListProperty;
myListProperty= DependencyProperty.RegisterAttached("ListSource", typeof(IList<int>), typeof(mypanel));

            var b = new Binding("ListSource") { Source = myList, Mode = BindingMode.TwoWay };
            SetBinding(LayerSourceProperty, b);

解决方案:以下事情有效..

public static DependencyProperty myListProperty=
            DependencyProperty.Register("ListSource", typeof(IList<int>), typeof(mypanel), new FrameworkPropertyMetadata(mylistchanged));


private static void LayerSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var obj = (mypanel) d;
    .......
}
4

2 回答 2

1

mylist您通常不会在派生 Panel中声明属性,而是将派生 Panel 放入 anItemsPanelTemplateItemsControl

<ItemsControl ItemsSource="{Binding DataItems}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <local:MyPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
于 2013-03-04T08:18:04.447 回答
0

正如 Nicolas 所说,只有依赖属性允许完整的数据绑定功能。

于 2013-03-04T07:59:10.583 回答