簡體   English   中英

WPF Repeater(like)控制收集源?

[英]WPF Repeater (like) control for collection source?

我有一個綁定到ObservableCollection的WPF DataGrid 我的集合中的每個項都有一個List<someObject> ,它是一個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>

但是,這似乎僅適用於更多靜態源,因為它會引發以下異常(集合在創建后不會更改):

ItemsStrol正在使用時,ItemsControl Operation無效。 使用ItemsControl.ItemsSource訪問和修改元素*

還有另一種方法來實現這一目標嗎?

你做了什么,通過指定<DataTemplate .../>里面ItemsControl為你添加的這個實例DataTemplate默認的屬性ItemsControlItems 因此,您獲得的異常是預期結果:首先指定ItemsSource ,然后修改Items 相反,您應該修改ItemsControl上的ItemTemplate屬性,如下所示:

<ItemsControl ItemsSource="{Binding Path=Exchanges}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Label>test</Label>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM