如何确保 WPF 中 ItemsControl 中的第一个项目仅显示标题?
我正在使用 MVVM 将子项的 ObservableCollection
绑定到 ItemsControl
。 ItemsControl
包含一个 UserControl,用于设置子级 UI 的样式。
<ItemsControl ItemsSource="{Binding Documents}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<View:DocumentView Margin="0, 10, 0, 0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我想显示 ItemsControl
内容的标题行,但只想在顶部显示一次(不是每个子项)。如何在 DocumentView
用户控件中实现此行为?仅供参考,我正在使用 Grid 布局来为子行设置样式:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="34"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="60" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.ColumnSpan="4" Grid.Row="0" Text="Should only show this at the top"></TextBlock>
<Image Grid.Column="0" Grid.Row="1" Height="24" Width="24" Source="/Beazley.Documents.Presentation;component/Icons/error.png"></Image>
<ComboBox Grid.Column="1" Grid.Row="1" Name="ContentTypes" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type View:MainView}}, Path=DataContext.ContentTypes}" SelectedValue="{Binding ContentType}"/>
<TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=FileName}"/>
<Button Grid.Column="3" Grid.Row="1"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type View:MainView}}, Path=DataContext.RemoveFile}"
CommandParameter="{Binding}">Remove</Button>
</Grid>
I am using MVVM binding an ObservableCollection
of children to an ItemsControl
. The ItemsControl
contains a UserControl used to style the UI for the children.
<ItemsControl ItemsSource="{Binding Documents}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<View:DocumentView Margin="0, 10, 0, 0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I want to show a header row for the contents of the ItemsControl
but only want to show this once at the top (not for every child). How can I implement this behaviour in the DocumentView
user control? Fyi I am using a Grid
layout to Style
the child rows:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="34"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="60" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.ColumnSpan="4" Grid.Row="0" Text="Should only show this at the top"></TextBlock>
<Image Grid.Column="0" Grid.Row="1" Height="24" Width="24" Source="/Beazley.Documents.Presentation;component/Icons/error.png"></Image>
<ComboBox Grid.Column="1" Grid.Row="1" Name="ContentTypes" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type View:MainView}}, Path=DataContext.ContentTypes}" SelectedValue="{Binding ContentType}"/>
<TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=FileName}"/>
<Button Grid.Column="3" Grid.Row="1"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type View:MainView}}, Path=DataContext.RemoveFile}"
CommandParameter="{Binding}">Remove</Button>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
再想一想,你为什么要自己尝试这样做呢?
看起来您基本上只是想创建一个具有自定义列类型的表...您尝试过 ListView 或 DataGrid 控件吗?
On second thoughts why are you even trying to do this yourself?
Looks like you are basically just trying to create a table with custom column types... Have you tried ListView or DataGrid controls?
您应该能够使用 AlternationCount 通过将 AlternationCount 设置为集合的大小来计算项目在 ItemsControl 中的位置。
然后将您的样式仅应用于 ItemsControl.AlternationIndex = 0 的项目。
假设您的集合大小为 5,ListBox(这是一个 ItemsControl)的示例:
毫无疑问,也有一种无需触发器即可实现的方法...
You should be able to use the AlternationCount to work out the position of the item in your ItemsControl by setting the AlternationCount to the size of your collection.
Then apply your style only to the Item where ItemsControl.AlternationIndex = 0.
An example for a ListBox (which is an ItemsControl) assuming your collection is 5 in size:
There is no doubt a way to do it without triggers too...