如何在WPF中使用派生面板进行数据绑定?

时间:2013-03-04 07:52:07

标签: c# wpf

我有一个派生面板,列表作为其成员。如何从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;
    .......
}

2 个答案:

答案 0 :(得分:1)

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

<ItemsControl ItemsSource="{Binding DataItems}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <local:MyPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

答案 1 :(得分:0)

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