项目集合必须在使用ItemsSource前空 [英] Items collection must be empty before using ItemsSource
问题描述
如果我把一个DataTrigger在一个简单的列表框我得到这个运行时异常:
产品集合必须在使用ItemsSource之前<空/ p>
块引用>
我的列表框中没有datatrigger(也不例外):
< ListBox中的ItemsSource ={结合EdgedBoards}的SelectedItem ={结合SelEdgedBoard,模式=双向}的DisplayMemberPath =名称>
< ListBox.ItemContainerStyle>
<风格的TargetType ={X:输入一个ListBoxItem}
支持算法FMP ={StaticResource的{X:输入一个ListBoxItem}}>
< setter属性=IsSelected
值={结合IsSelected,模式=双向,UpdateSourceTrigger =的PropertyChanged}/>
< /样式和GT;
< /ListBox.ItemContainerStyle>
< /列表框>
我的列表框与
DataTrigger
>
< ListBox中的ItemsSource ={结合EdgedBoards}的SelectedItem ={结合SelEdgedBoard,模式=双向}的DisplayMemberPath =名称> ;
<风格的TargetType ={X:类型列表框}支持算法FMP ={StaticResource的{X:类型列表框}}>
< setter属性=调焦VALUE =真/>
< Style.Triggers>
< DataTrigger绑定={绑定的ElementName = EdgedBoardsAdd_UC,路径=能见度}VALUE =可见>
< setter属性=调焦VALUE =FALSE/>
< / DataTrigger>
< /Style.Triggers>
< /样式和GT;
< ListBox.ItemContainerStyle>
<风格的TargetType ={X:输入一个ListBoxItem}支持算法FMP ={StaticResource的{X:输入一个ListBoxItem}}>
< setter属性=IsSelectedVALUE ={结合IsSelected,模式=双向,UpdateSourceTrigger =的PropertyChanged}/>
< /样式和GT;
< /ListBox.ItemContainerStyle>
< /列表框>
这有什么错后者的代码?
解决方案您未正确申报的风格,所以它被设置为的内容的ListBox的 - 你是手工声明包含一个样式列表
您应该与
<包装现有的
元素来解决这个问题。风格
元素; ListBox.Style>If I put a DataTrigger in a simple Listbox I get this runtime exception:
Items collection must be empty before using ItemsSource
My listbox without datatrigger (no exception):
<ListBox ItemsSource="{Binding EdgedBoards}" SelectedItem="{Binding SelEdgedBoard, Mode=TwoWay}" DisplayMemberPath="Name"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </Style> </ListBox.ItemContainerStyle> </ListBox>
My ListBox with
DataTrigger
:<ListBox ItemsSource="{Binding EdgedBoards}" SelectedItem="{Binding SelEdgedBoard, Mode=TwoWay}" DisplayMemberPath="Name"> <Style TargetType="{x:Type ListBox}" BasedOn="{StaticResource {x:Type ListBox}}"> <Setter Property="Focusable" Value="True" /> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=EdgedBoardsAdd_UC, Path=Visibility}" Value="Visible"> <Setter Property="Focusable" Value="False" /> </DataTrigger> </Style.Triggers> </Style> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </Style> </ListBox.ItemContainerStyle> </ListBox>
What's wrong with the latter code?
解决方案You aren't declaring the style correctly, and so it's being set as the content of the listbox - you are manually declaring a list that contains a single style.
You should wrap your existing
Style
element with the<ListBox.Style>
element to fix this problem.这篇关于项目集合必须在使用ItemsSource前空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!