【问题标题】:How to group items inside custom control combobox?如何在自定义控件组合框中对项目进行分组?
【发布时间】:2011-08-07 18:17:08
【问题描述】:

我一直在尝试使用 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 网格中进行 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>

【问题讨论】:

    标签: wpf combobox grouping collectionviewsource


    【解决方案1】:

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

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

    【讨论】:

    • 我已经编辑了这个问题 - 我发现这可以使用控件外部的资源来解决,但我确信可以在内部进行,我只是不确定如何!!
    • 嗯,我在这里有点困惑......你想对集合视图源做什么(即据我了解的数据分组:))和编辑之间的联系是什么?你的帖子......我已经重新阅读了你最初的问题,也许我在这里缺少的是“但我想将所有代码移动到 GroupedImageComboBox 控件”部分......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多