WPF Repeater(like)控制收集源?

Son*_*oul 30 .net c# wpf

我有一个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>
Run Code Online (Sandbox Code Playgroud)

等等0-n次.

我试过用ItemsControl这个:

<ItemsControl ItemsSource="{Binding Path=Exchanges}">
    <DataTemplate>
        <Label>test</Label>
    </DataTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

但是,这似乎仅适用于更多静态源,因为它会引发以下异常(集合在创建后不会更改):

ItemsStrol正在使用时,ItemsControl Operation无效.使用ItemsControl.ItemsSource访问和修改元素*

还有另一种方法来实现这一目标吗?

rep*_*pka 67

你所做的,通过指定<DataTemplate .../>的里面ItemsControl是你添加的这种情况下DataTemplate,以默认属性的ItemsControlItems.所以你得到的例外是预期的结果:首先你指定ItemsSource,然后你修改Items.相反,你应该修改ItemTemplateItemsControl喜欢的属性,所以:

<ItemsControl ItemsSource="{Binding Path=Exchanges}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Label>test</Label>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!哇..没什么关于那是直观的:) (2认同)