首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于收集源的WPF Repeater (类似)控件?

用于收集源的WPF Repeater (类似)控件?
EN

Stack Overflow用户
提问于 2010-06-09 21:35:21
回答 1查看 26.9K关注 0票数 30

我有一个绑定到DataGrid的WPF DataGrid。集合中的每个项都具有属性,即List<someObject>。在“行详细信息”窗格中,我希望为此集合中的每个项写出格式化文本块。最终结果将相当于:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<ItemsControl ItemsSource="{Binding Path=Exchanges}">
    <DataTemplate>
        <Label>test</Label>
    </DataTemplate>
</ItemsControl>

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

ItemsControl操作在使用ItemsSource时无效。使用ItemsControl.ItemsSource访问和修改元素*

还有其他方法可以做到这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-09 22:03:25

通过在<DataTemplate .../>中指定ItemsControl,您所做的就是将DataTemplate的这个实例添加到ItemsControl的默认属性中,即Items。因此,您得到的例外是预期的结果:首先指定ItemsSource,然后修改Items。相反,您应该修改ItemTemplate属性在ItemsControl上,如下所示:

代码语言:javascript
复制
<ItemsControl ItemsSource="{Binding Path=Exchanges}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Label>test</Label>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
票数 68
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3010131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档