【问题标题】:Grouping items disregards items style ( Groups show up empty )分组项目忽略项目样式(组显示为空)
【发布时间】:2011-04-05 19:35:55
【问题描述】:

这基本上是我上一个问题here的后续问题

我正在尝试对组合框中的项目进行分组,但我的问题是显示 GroupDescription 或项目。

如果我添加collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Team"));,那么我的 ComboBoxItem 样式将被忽略(它永远不会到达断点)。这是为什么呢?

我检查了collectionView,它包含 3 个组,每个组有 2 个项目。但下拉菜单只显示“分组名称”(即团队名称)。

******编辑******

问题似乎出在我的 ComboBoxStyle 中,因为删除它让生活变得美好......

<Style x:Key="ImageComboBox" BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="{x:Type ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
                        <ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{StaticResource DisplayImageWithText}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                        <TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,3,23,3" Focusable="True" Background="Transparent" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}"/>
                        <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
                            <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border x:Name="DropDownBorder" Background="{StaticResource WindowBackgroundBrush}" BorderThickness="1" BorderBrush="{StaticResource SolidBorderBrush}"/>
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                    <Grid x:Name="itemsGrid" Height="Auto" Width="Auto" MaxWidth="{TemplateBinding MaxWidth}">
                                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
                                    </Grid>
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="false">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                            <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                            <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                        </Trigger>
                        <Trigger Property="IsEditable" Value="true">
                            <Setter Property="IsTabStop" Value="false"/>
                            <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【问题讨论】:

  • 您是否为 Items 指定了 ItemTemplate?
  • 在 ComboBoxItemStyle 我有 ...
  • 在下面查看我的答案。希望能帮助到你。如果它不能帮助您解决问题,也许您可​​以发布有关您的问题的更多信息,例如“ComboBoxItemStyle”的 XAML,以及您的 ComboBox 实例的完整 XAML。

标签: wpf combobox grouping listcollectionview


【解决方案1】:

根据您在上面提供的信息,我不确定是什么导致了您的问题。但是下面的代码是对我在您指定的链接上给出的答案的更新,对我来说效果很好:

样式:

<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
    <Setter Property="Foreground" Value="Red"/>
</Style>

<Style x:Key="ComboBoxStyle" BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyle}"/>
</Style>

组合框:

<ComboBox x:Name="comboBox" Style="{StaticResource ComboBoxStyle}">
    <ComboBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ComboBox.GroupStyle>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

希望这能帮助您解决问题。

【讨论】:

  • 你的回答和评论做了一些改变,它的恶棍是 ComboBoxStyle...用完整的样式代码更新了问题
  • 我看到,在您的 ComboBoxStyle 中,将 更改为 。我打赌这应该可以解决它。让我知道是否有。 =)
  • 就像一个魅力。它总是应该是最低级别的 ItemsPresenter 还是?
  • 不一定。但通常应该使用 ItemsPresenter。这将确保即使是组也可以工作。有关更多信息,请参阅此链接:social.msdn.microsoft.com/forums/en-US/wpf/thread/…
猜你喜欢
  • 2019-11-19
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 2020-07-23
  • 2019-10-11
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多