Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用WPF中的派生面板将数据绑定到列表?_C#_Wpf - Fatal编程技术网

C# 如何使用WPF中的派生面板将数据绑定到列表?

C# 如何使用WPF中的派生面板将数据绑定到列表?,c#,wpf,C#,Wpf,我有一个派生面板,其成员为列表。如何从xaml绑定到列表 class mypanel : Panel { IList<int> mylist; ... } class mypanel:Panel { 伊利斯特糜棱岩; ... } 编辑: 公共静态DependencyProperty myListProperty; myListProperty=DependencyProperty.RegisterAttached(“ListSource”、typeof(IList

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

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>