WPF ComboBox с несколькими ItemsPresenter - PullRequest
0 голосов
/ 22 октября 2019

Мне нужно создать пользовательский ComboBox с 2 ItemsPresenter, но я не знаю, как установить ItemsPresenter, куда добавлять и удалять элементы.

Я пытался получить ItemsPresenter:

ItemsPresenter itemsPresenter = (ItemsPresenter)comboBoxAlbum.FindName("ItemsPresenterAlbum");

но я не знаю, как получить коллекцию Items.

Вот как я изменил часть Popup:

<Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
    <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
        <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
            <Grid Background="White">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="100*"/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" Background="#FF929CA4">
                    <ScrollViewer Background="#FF929CA4">
                        <Grid x:Name="ToolBar" SnapsToDevicePixels="True" Background="#FF929CA4">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <StackPanel x:Name="btnStretchNone" MouseUp="btnStretch_MouseUp" Grid.Column="0" Background="{Binding BtnStretchNone}" Style="{DynamicResource StakPanelEffect}">
                                <Image Width="32" Height="32" Margin="5" Source="Resources/StretchNone.png"/>
                            </StackPanel>
                            <StackPanel x:Name="btnStretchFill" MouseUp="btnStretch_MouseUp" Grid.Column="1" Background="{Binding BtnStretchFill}" Style="{DynamicResource StakPanelEffect}">
                                <Image Width="32" Height="32" Margin="5" Source="Resources/StretchFill.png"/>
                            </StackPanel>
                            <StackPanel x:Name="btnStretchUniform" MouseUp="btnStretch_MouseUp" Grid.Column="2" Background="{Binding BtnStretchUniform}" Style="{DynamicResource StakPanelEffect}">
                                <Image Width="32" Height="32" Margin="5" Source="Resources/StretchUniform.png"/>
                            </StackPanel>
                            <StackPanel x:Name="btnStretchUniformFill" MouseUp="btnStretch_MouseUp" Grid.Column="3" Background="{Binding BtnStretchUniformFill}" Style="{DynamicResource StakPanelEffect}">
                                <Image Width="32" Height="32" Margin="5" Source="Resources/StretchUniformFill.png"/>
                            </StackPanel>
                            <StackPanel x:Name="btnSettings" MouseUp="btnSettings_MouseUp" Grid.Column="4" Background="{Binding BtnStretchFrame}" Style="{DynamicResource StakPanelEffect}" IsEnabled="False">
                                <Image Width="32" Height="32" Margin="5" Source="Resources/SettingsB.png"/>
                            </StackPanel>
                        </Grid>
                    </ScrollViewer>
                </Grid>
                <Grid Grid.Row="1">
                    <TabControl Height="{Binding ActualHeight, ConverterParameter=-42, Converter={StaticResource AdditionConverter}, ElementName=DropDownBorder}">
                        <TabItem Header="Albums">
                            <ScrollViewer x:Name="DropDownScrollViewerAlbum">
                                <Grid x:Name="gridAlbum" RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas x:Name="canvasAlbum" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                        <Rectangle x:Name="OpaqueRectAlbum" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ConverterParameter=-72, Converter={StaticResource AdditionConverter}, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ConverterParameter=-8, Converter={StaticResource AdditionConverter}, ElementName=DropDownBorder}"/>
                                    </Canvas>
                                    <ItemsPresenter x:Name="ItemsPresenterAlbum" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </ScrollViewer>
                        </TabItem>
                        <TabItem Header="Slides">
                            <ScrollViewer x:Name="DropDownScrollViewerSlide">
                                <Grid x:Name="gridSlide" RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas x:Name="canvasSlide" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                        <Rectangle x:Name="OpaqueRectSlide" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ConverterParameter=-72, Converter={StaticResource AdditionConverter}, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ConverterParameter=-8, Converter={StaticResource AdditionConverter}, ElementName=DropDownBorder}"/>
                                    </Canvas>
                                    <ItemsPresenter x:Name="ItemsPresenterSlide" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </ScrollViewer>
                        </TabItem>
                    </TabControl>
                </Grid>
            </Grid>
        </Border>
    </Themes:SystemDropShadowChrome>
</Popup>

Есть 2 ItemsPresenter (ItemsPresenterAlbum, ItemsPresenterSlide). Как я могу добавить предметы в определенный ItemsPresenter? Используя Items.Add () он добавляет только ко второму ItemsPresenter.

...