我有一个绑定到DataGrid的WPF DataGrid。集合中的每个项都具有属性,即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>但是,这似乎只适用于更多的静态源,因为它引发以下异常(创建后不会更改集合):
ItemsControl操作在使用ItemsSource时无效。使用ItemsControl.ItemsSource访问和修改元素*
还有其他方法可以做到这一点吗?
发布于 2010-06-09 22:03:25
通过在<DataTemplate .../>中指定ItemsControl,您所做的就是将DataTemplate的这个实例添加到ItemsControl的默认属性中,即Items。因此,您得到的例外是预期的结果:首先指定ItemsSource,然后修改Items。相反,您应该修改ItemTemplate属性在ItemsControl上,如下所示:
<ItemsControl ItemsSource="{Binding Path=Exchanges}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label>test</Label>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>https://stackoverflow.com/questions/3010131
复制相似问题