如何确保 WPF 中 ItemsControl 中的第一个项目仅显示标题?

发布于 2024-08-21 02:51:41 字数 1869 浏览 7 评论 0原文

我正在使用 MVVM 将子项的 ObservableCollection 绑定到 ItemsControlItemsControl 包含一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-08-28 02:51:41

再想一想,你为什么要自己尝试这样做呢?

看起来您基本上只是想创建一个具有自定义列类型的表...您尝试过 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?

夜未央樱花落 2024-08-28 02:51:41

您应该能够使用 AlternationCount 通过将 AlternationCount 设置为集合的大小来计算项目在 ItemsControl 中的位置。

然后将您的样式仅应用于 ItemsControl.AlternationIndex = 0 的项目。

假设您的集合大小为 5,ListBox(这是一个 ItemsControl)的示例:

<Grid>
  <Grid.Resources>
    <Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="Foreground" Value="White"/>
      <Style.Triggers>
        <Trigger Property="ListBox.AlternationIndex" Value="0">
          <Setter Property="Background" Value="CornflowerBlue"/>
          <Setter Property="Foreground" Value="Black"/>
        </Trigger>
      </Style.Triggers>
    </Style>

  </Grid.Resources>
  <ListBox AlternationCount="5" ItemsSource="{StaticResource data}" 
           ItemContainerStyle="{StaticResource alternatingWithTriggers}">
  </ListBox>
</Grid>

毫无疑问,也有一种无需触发器即可实现的方法...

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:

<Grid>
  <Grid.Resources>
    <Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="Foreground" Value="White"/>
      <Style.Triggers>
        <Trigger Property="ListBox.AlternationIndex" Value="0">
          <Setter Property="Background" Value="CornflowerBlue"/>
          <Setter Property="Foreground" Value="Black"/>
        </Trigger>
      </Style.Triggers>
    </Style>

  </Grid.Resources>
  <ListBox AlternationCount="5" ItemsSource="{StaticResource data}" 
           ItemContainerStyle="{StaticResource alternatingWithTriggers}">
  </ListBox>
</Grid>

There is no doubt a way to do it without triggers too...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文