Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
Wpf ItemsControl ItemTemplate绑定_Wpf_Binding_.net 4.0_Itemscontrol_Itemtemplate - Fatal编程技术网

Wpf ItemsControl ItemTemplate绑定

Wpf ItemsControl ItemTemplate绑定,wpf,binding,.net-4.0,itemscontrol,itemtemplate,Wpf,Binding,.net 4.0,Itemscontrol,Itemtemplate,在WPF4.0中,我有一个包含其他类类型作为属性的类(组合多个数据类型以显示)。比如: public partial class Owner { public string OwnerName { get; set; } public int OwnerId { get; set; } } partial class ForDisplay { public Owner OwnerData { get; set; } public int Credi

在WPF4.0中,我有一个包含其他类类型作为属性的类(组合多个数据类型以显示)。比如:

public partial class Owner
{
     public string OwnerName { get; set; }
     public int    OwnerId   { get; set; }
}

partial class ForDisplay
{
    public Owner OwnerData { get; set; }
    public int Credit { get; set; }
}
在我的窗口中,我有一个ItemsControl,其中包含以下内容(为清晰起见进行了剪裁):


您确定您在此发布的代码就是您在解决方案中使用的代码吗?因为,这段代码对我很有用:

XAML


窗口的已加载事件

ObservableCollection<ForDisplay> items = new ObservableCollection<ForDisplay>();

for (int i = 0; i < 10; i++)
{
    items.Add(new ForDisplay() { OwnerData = new Owner() { OwnerId = i + 1, OwnerName = String.Format("Owner #{0}", i + 1) }, Credit = i + 1 });
}

DataContext = items;
observetecollection items=新的observetecollection();
对于(int i=0;i<10;i++)
{
添加(newfordisplay(){OwnerData=newowner(){OwnerId=i+1,OwnerName=String.Format(“Owner{0},i+1)},Credit=i+1});
}
DataContext=项目;

不知何故,它试图在ForDisplay对象上找到OwnerName属性。使用以下链接进行调试:。还有一个问题,MyDisplayControl上的OwnerName属性是依赖性属性吗?我看不出有什么问题,假设裁剪的内容没有隐藏任何相关内容。您是否尝试过清理/重建解决方案?我讨厌这个建议,因为它相当于告诉你“重新启动”,但我有一个数据绑定的情况,应该工作,但没有,我发现清理/重建有时会修复它。不确定原因。一个初步猜测:更改绑定以显式设置路径。ie“OwnerName={Binding Path=OwnerData.OwnerName}”我的设计也是这样。但是它在
上抛出NullReferenceException,我认为这与自定义控件有关。参见问题的编辑。
<ItemsControl ItemsSource={Binding}>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
          <StackPanel>
              <local:MyDisplayControl 
                        OwnerName={Binding OwnerData.OwnerName}
                        Credit={Binding Credit} />
              <TextBlock Text="{Binding OwnerData.OwnerName}" />
              <TextBlock Text="{Binding Credit}" />
          </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding OwnerData.OwnerName}"></TextBlock>
                <TextBlock Text="{Binding Credit}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
ObservableCollection<ForDisplay> items = new ObservableCollection<ForDisplay>();

for (int i = 0; i < 10; i++)
{
    items.Add(new ForDisplay() { OwnerData = new Owner() { OwnerId = i + 1, OwnerName = String.Format("Owner #{0}", i + 1) }, Credit = i + 1 });
}

DataContext = items;