C# 如何使用WPF中的派生面板将数据绑定到列表?
我有一个派生面板,其成员为列表。如何从xaml绑定到列表C# 如何使用WPF中的派生面板将数据绑定到列表?,c#,wpf,C#,Wpf,我有一个派生面板,其成员为列表。如何从xaml绑定到列表 class mypanel : Panel { IList<int> mylist; ... } class mypanel:Panel { 伊利斯特糜棱岩; ... } 编辑: 公共静态DependencyProperty myListProperty; myListProperty=DependencyProperty.RegisterAttached(“ListSource”、typeof(IList
class mypanel : Panel
{
IList<int> mylist;
...
}
class mypanel:Panel
{
伊利斯特糜棱岩;
...
}
编辑:
公共静态DependencyProperty myListProperty;
myListProperty=DependencyProperty.RegisterAttached(“ListSource”、typeof(IList)、typeof(mypanel));
var b=newbinding(“ListSource”){Source=myList,Mode=BindingMode.TwoWay};
收进绑定(层资源属性,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;
.......
}
公共静态从属属性myListProperty=
Register(“ListSource”、typeof(IList)、typeof(mypanel)、new FrameworkPropertyMetadata(mylistchanged));
私有静态无效层资源已更改(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var obj=(mypanel)d;
.......
}
正如Nicolas所说,只有依赖项属性才允许完整的数据绑定功能 与在派生面板中声明mylist
属性不同,您通常会将派生面板放入一个
“mylist”是依赖项属性吗?是。我已经创建了一个依赖属性。我不熟悉C#和WPF,我不太确定我是否做得正确。请使用Register
而不是RegisterAttached
。和谷歌的“依赖属性”和“附加依赖属性”的区别:)@nicolasrepiquiet谢谢。你建议的解决方案奏效了。
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;
.......
}
<ItemsControl ItemsSource="{Binding DataItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:MyPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>