如何在自定义控件组合框内对项目进行分组?

时间:2011-04-18 10:05:46

标签: wpf combobox grouping collectionviewsource

我一直在尝试使用xaml对我的自定义控件组合框的项目进行分组,而且我有点卡住了。我一直在阅读并发现以下代码产生我想要的结果,但我想将所有代码移动到GroupedImageComboBox控件中。

<StackPanel>

        <StackPanel.Resources>
            <CollectionViewSource x:Key="groupedData" Source="{Binding Items}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="EntityBaseDependencyType" Converter="{StaticResource enumConverter}"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </StackPanel.Resources>
        <WPFControls:GroupedImageComboBox  
                                         ItemsSource ="{Binding Source={StaticResource groupedData}}"     
                                          SelectedItem="{Binding SelectedItem}" 
                                      >
            <ItemsControl.GroupStyle>
                <x:Static Member="GroupStyle.Default"/>
            </ItemsControl.GroupStyle>
        </WPFControls:GroupedImageComboBox>

    </StackPanel>

我希望能够从该区域中删除stackpanel和itemscontrol的使用,并将它们放在GroupedImageComboBox中。有没有办法实现这个目标?

提前致谢。

编辑1。

我一直在玩模板,觉得我应该能够在DropDown网格中进行收集视图操作 - 我只是不确定如何....

<Style TargetType="{x:Type WPFControls:ImageComboBox}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="ItemContainerStyle" Value="{StaticResource CustomComboBoxItemStyle}"/>
    <Setter Property="IsSynchronizedWithCurrentItem" Value="true"/>
    <Setter Property="Margin" Value="8"/>
    <Setter Property="IsEditable" Value="False"/>
    <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">
                    </ToggleButton>
                    <ContentPresenter Name="ContentSite"
                                      IsHitTestVisible="False" 
                                      Content="{TemplateBinding SelectionBoxItem}"
                                      ContentTemplate="{StaticResource DiplayImageWithTextDataTemplate}"
                                      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">
                                <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained"
                                                />
                            </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.Triggers>
    </Style.Triggers>
</Style>

1 个答案:

答案 0 :(得分:0)

实际上,似乎在这个例子中,堆栈面板不是你必须要使用的东西。

只需将CollectionViewSource声明移动到其他位置(例如,在</Window.Resources><Window.Resources>标记中),然后将组合框放在任何您想要的位置。