gpt4 book ai didi

c# - 收集源的 WPF Repeater(类似)控件?

转载 作者:IT王子 更新时间:2023-10-29 04:10:38 27 4
gpt4 key购买 nike

我有一个 WPF DataGrid绑定(bind)到 ObservableCollection .我收藏中的每件元素都有属性,即 List<someObject> .在我的行详细信息 Pane 中,我想为该集合中的每个项目写出格式化的文本 block 。最终结果将等同于:

<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 Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead*

还有其他方法可以实现吗?

最佳答案

您通过指定 <DataTemplate .../> 做了什么ItemsControl 内部你添加了这个 DataTemplate 的实例吗? ItemsControl 的默认属性这是 Items .所以你得到的异常是预期的结果:首先你指定 ItemsSource , 然后你修改 Items .相反,您应该修改 ItemTemplate您的属性(property)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>

关于c# - 收集源的 WPF Repeater(类似)控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3010131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com