首页 > 解决方案 > 如何在 Dropbox 中添加子项

问题描述

我有一个未完成的项目,我的第一步是修改基于组合框的主菜单,看起来像 GitHub 桌面应用程序

默认视图

所以现在的任务是将相似的项目分组到子组中。

例如,日志视图城市视图商店视图应放在管理组下。2017...2020 年应归为一组。

不幸的是我找不到这个任务的解决方案,也许 ComboBox 不是最好的解决方案。也许有人可以告诉我我的解决方案在哪里?当嵌套项目出现在组的右侧时,它可以是可折叠组或类似菜单样式。

PS管理年表管理只是空的组合框项目。

标签: c#wpfxaml

解决方案


所以,我的解决方案是基于他们https://stackoverflow.com/a/3585244/7283900

我在我的 ComboBox 中添加了 Expander,所以整个解决方案现在看起来像

XAML:

<Window x:Class="WpfApp1_TEST.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1_TEST"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>

    <Grid>
        <Grid.Resources>
            <Style x:Key="GroupItem" TargetType="{x:Type GroupItem}">
                <Setter Property="Margin" Value="0,0,0,5"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander x:Name="expander" 
                                      ExpandDirection="Down">

                                <Expander.Header>
                                    <DockPanel>
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="2,5,0,2" FontSize="14"/>
                                    </DockPanel>
                                </Expander.Header>
                                <Expander.Content>
                                    <Border Margin="5,0,0,0">
                                        <ItemsPresenter >
                                        </ItemsPresenter>
                                    </Border>
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <ComboBox Height="27" HorizontalAlignment="Left"  
                  ItemsSource="{Binding Item}"
                  Margin="162,109,0,0" VerticalAlignment="Top" 
                  Width="195" Name="cboGroup" >
            <ComboBox.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupItem}"/>
            </ComboBox.GroupStyle>
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>

</Grid>

以及此页面的 CodeBehind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<Item> items = new List<Item>();
        items.Add(new Item() { Name = "Item1", Category = "Category_1" });
        items.Add(new Item() { Name = "Item2", Category = "Category_1" });
        items.Add(new Item() { Name = "Item3", Category = "Category_1" });
        items.Add(new Item() { Name = "Item4", Category = "Category_2" });
        items.Add(new Item() { Name = "Item5", Category = "Category_2" });

        ListCollectionView lcv = new ListCollectionView(items);
        lcv.GroupDescriptions.Add(new PropertyGroupDescription("Category"));

        //this.comboBox.ItemsSource = lcv;
        this.cboGroup.ItemsSource = lcv;
    }

    public class Item
    {
        public string Name { get; set; }
        public string Category { get; set; }
    }


}

推荐阅读