如何将自定义控件组合框内的项目分组?

问题描述:

我一直在使用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.

我一直在玩的模板,并认为我应该可以下拉网格内做的CollectionView操作 - 我只是不知道如何....

<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> 

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

只需推动CollectionViewSource申报的其他地方(在你</Window.Resources><Window.Resources>标签为例),把你的组合框其他地方你想要的。

+0

我编辑的问题 - 我看到,这可以通过控制外部资源来解决,但我敢肯定,这是可能做到这里面,我只是不知道如何! – user713169 2011-04-18 14:35:01

+0

嗯,我有点困惑在这里...什么是你的文章的编辑你想要做集合视图源(即数据分组,据我了解:))之间有什么联系?我有重读您最初的问题,也许就是我在这里失踪的是“但我想将所有的代码放到GroupedImageComboBox控制”的一部分... – Bruno 2011-04-18 21:00:43