【发布时间】:2010-06-09 21:35:21
【问题描述】:
我有一个 WPF DataGrid 绑定到 ObservableCollection。
我收藏中的每个项目都有属性,即List<someObject>。
在我的行详细信息窗格中,我想为此集合中的每个项目写出格式化的文本块。最终结果将相当于:
<TextBlock Style="{StaticResource NBBOTextBlockStyle}" HorizontalAlignment="Right">
<TextBlock.Inlines>
<Run FontWeight="Bold" Text="{Binding Path=Exchanges[0].Name}" />
<Run FontWeight="Bold" Text="{Binding Path=Exchanges[0].Price}" />
<LineBreak />
<Run Foreground="LightGray" Text="{Binding Path=Exchanges[0].Quantity}" />
</TextBlock.Inlines>
</TextBlock>
<TextBlock Style="{StaticResource NBBOTextBlockStyle}">
<TextBlock.Inlines>
<Run FontWeight="Bold" Text="{Binding Path=Exchanges[1].Name}" />
<Run FontWeight="Bold" Text="{Binding Path=Exchanges[1].Price}" />
<LineBreak />
<Run Foreground="LightGray" Text="{Binding Path=Exchanges[1].Quantity}" />
</TextBlock.Inlines>
</TextBlock>
以此类推 0-n 次。
我已经尝试使用 ItemsControl 来解决这个问题:
<ItemsControl ItemsSource="{Binding Path=Exchanges}">
<DataTemplate>
<Label>test</Label>
</DataTemplate>
</ItemsControl>
但是,这似乎仅适用于更多静态源,因为它会引发以下异常(集合在创建后不会更改):
ItemsSource 正在使用时,ItemsControl 操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素*
还有其他方法可以实现吗?
【问题讨论】:
-
ItemsControl 应该没问题。在绑定 ItemsSource 的情况下使用 ItemsControl 的 Items 属性时,通常会出现此错误,可能是这种情况吗?