Zac*_*son 162 .net vb.net wpf binding entity-framework
我正在尝试将图像显示在WPF ListView中,其风格类似于WrapPanel,如旧的ATC Avalon团队文章中所述:如何创建自定义视图.

当我尝试使用LINQ-to-Entities查询的ADO.NET Entity Framework对象集合填充ListView时,我得到以下异常:
例外
在使用ItemsSource之前,项集合必须为空.
我的代码......
Visual Basic
Private Sub Window1_Loaded(...) Handles MyBase.Loaded
ListViewImages.ItemsSource = From g In db.Graphic _
Order By g.DateAdded Ascending _
Select g
End Sub
Run Code Online (Sandbox Code Playgroud)
XAML
<ListView Name="ListViewImages"
SelectionMode="Single"
ItemsSource="{Binding}">
<local:ImageView />
</ListView>
Run Code Online (Sandbox Code Playgroud)
我在那条线上放了一个断点. ListViewImages.ItemsSource是Nothing刚刚LINQ分配之前.
ken*_*ner 178
在一个稍微不同的场景中,我有一段时间出现同样的错误.我有
<wpftoolkit:DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Accounts}" >
<wpftoolkit:DataGridTextColumn
Header="Account Name"
Binding="{Binding Path=AccountName}" />
</wpftoolkit:DataGrid>
Run Code Online (Sandbox Code Playgroud)
我固定的是
<wpftoolkit:DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Accounts}" >
<wpftoolkit:DataGrid.Columns>
<wpftoolkit:DataGridTextColumn
Header="Account Name"
Binding="{Binding Path=AccountName}" />
</wpftoolkit:DataGrid.Columns>
</wpftoolkit:DataGrid>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ave 119
抛出此特定异常的原因是元素的内容应用于ListView的Items集合.因此,XAML在其Items集合中使用单个本地:ImageView初始化ListView.但是当使用ItemsControl时,您必须使用Items属性或ItemsSource属性,不能同时使用它们.因此,当处理ItemsSource属性时,将引发异常.
您可以通过查找类中的ContentPropertyAttribute来找出元素内容将应用于哪个属性.在这种情况下,它在类层次结构中的ItemsControl上定义得更高:
[ContentPropertyAttribute("Items")]
Run Code Online (Sandbox Code Playgroud)
这里的意图是将ListView的View设置为local:ImageView,因此修复是明确指示要设置的属性.
修复XAML,异常消失:
<ListView Name="ListViewImages"
SelectionMode="Single"
ItemsSource="{Binding}">
<ListView.View>
<local:ImageView />
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
它缺少那个<ListView.View>标签.
Arm*_*age 64
我刚刚遇到了这个问题的一个非常阴险的例子.我的原始片段要复杂得多,这使得很难看到错误.
<ItemsControl
Foreground="Black" Background="White" Grid.IsSharedSizingScope="True"
x:Name="MyGrid" ItemsSource="{Binding}">
>
<ItemsControl.ItemsPanel>
<!-- All is fine here -->
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<!-- All is fine here -->
</ItemsControl.ItemTemplate>
<!-- Have you caught the error yet? -->
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
错误?初始开标签后的额外><ItemsControl>!已<应用于内置Items集合.稍后设置DataContext时,即时crashola.因此,在调试此问题时,请注意不仅仅是围绕ItemsControl特定数据子项的错误.
Jun*_*r M 39
我也是一个不同的场景.
<ComboBox Cursor="Hand" DataContext="{Binding}"
FontSize="16" Height="27" ItemsSource="{Binding}"
Name="cbxDamnCombo" SelectedIndex="0" SelectedValuePath="MemberId">
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}">
<Binding Path="MemberName"/>
<Binding Path="Phone"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
现在当你完成缺少的标签Control.ItemTemplate时,一切都恢复正常:
<ComboBox Cursor="Hand" DataContext="{Binding}"
FontSize="16" Height="27" ItemsSource="{Binding}"
Name="cbxDamnCombo" SelectedIndex="0" SelectedValuePath="MemberId">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}">
<Binding Path="MemberName"/>
<Binding Path="Phone"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
<ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
小智 26
我在不同的场景中遇到了同样的错误
<ItemsControl ItemsSource="{Binding TableList}">
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
解决方案是在ItemsControl.ItemsPanel之前添加标签ItemsPanelTemplate
<ItemsControl ItemsSource="{Binding TableList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
Biz*_*han 12
例外
在使用ItemsSource之前,项集合必须为空.
向ItemsSource 不同来源添加项目时会发生此异常.因此,请确保您没有意外遗漏标签,错误放置标签,添加额外标签或错误编写标签.
<!--Right-->
<ItemsControl ItemsSource="{Binding MyItems}">
<ItemsControl.ItemsPanel.../>
<ItemsControl.MyAttachedProperty.../>
<FrameworkElement.ActualWidth.../>
</ItemsControl>
<!--WRONG-->
<ItemsControl ItemsSource="{Binding MyItems}">
<Grid.../>
<Button.../>
<DataTemplate.../>
<Heigth.../>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
虽然ItemsControl.ItemsSource已经设置Binding,但其他项目(网格,按钮,...)无法添加到源.但是,ItemsSource如果未使用,则允许使用以下代码:
<!--Right-->
<ItemsControl>
<Button.../>
<TextBlock.../>
<sys:String.../>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
注意缺失的ItemsSource="{Binding MyItems}"部分.
Ωme*_*Man 11
以不同的方式陈述答案; 验证没有丢失的父节点或不正确的节点.
ItemsPanelTemplate子节点没有父(或错误的父)
<ItemsControl ItemsSource="{Binding TimeSpanChoices}">
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
...
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
<ItemsControl ItemsSource="{Binding TimeSpanChoices}">
<ItemsControl.ItemsPanel> <!-- I am the missing parent! -->
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
...
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
就我而言,它没有为 ItemsControl 使用 DataTemplate。
老的:
<ItemsControl Width="243" ItemsSource="{Binding List, Mode=TwoWay}">
<StackPanel Orientation="Horizontal">
<TextBox Width="25" Margin="0,0,5,0" Text="{Binding Path=Property1}"/>
<Label Content="{Binding Path=Property2}"/>
</StackPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
新的:
<ItemsControl Width="243" ItemsSource="{Binding List, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="25" Margin="0,0,5,0" Text="{Binding Path=Property1}"/>
<Label Content="{Binding Path=Property2}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
144335 次 |
| 最近记录: |