我有一个派生面板,列表作为其成员。如何从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;
.......
}
答案 0 :(得分:1)
您通常会将派生的Panel放入ItemsPanelTemplate
的ItemsControl
,而不是在派生的Panel中声明mylist
属性:
<ItemsControl ItemsSource="{Binding DataItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:MyPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
答案 1 :(得分:0)
正如Nicolas所说,只有依赖属性允许完整的数据绑定功能。