динамически связывать вложенный список с вложенной наблюдаемой коллекцией в wpf - PullRequest
0 голосов
/ 25 января 2020

Мне нужно создать ListView, чей ListItem может дополнительно содержать другой ListView (структура, подобная подменю меню). Я использую наблюдаемую коллекцию, чтобы связать эти списки. Моя проблема заключается в том, что мне нужно выяснить, сколько уровней ObservableCollections присутствует в основной ObservableCollection, основываясь на количестве, которое мне нужно для создания уровня ListView и связать эти observablecollection как источник элемента.

MenuItems.cs:

        public class MenuItems
        {
            private string menuname;
            public string MenuName
            {
                get { return menuname; }
                set { menuname = value; }
            }
            public ObservableCollection<SubMenuItems> subMenu;
        }
        public class SubMenuItems
        {
            private string subMenuName;
            public string SubMenuName
            {
                get { return subMenuName; }
                set { subMenuName = value; }
            }
        }
    ```
currently I have only two classes for 2 levels (Main menu items and sub menu items).likewise I can have n level of classes.

ViewModel:     
    public class NavigationViewModel
    {
        private ObservableCollection<MenuItems> _menuItems;
        public ObservableCollection<MenuItems> MainMenu
        {
            get { return _menuItems; }
            set { _menuItems = value; }
        }

        public NavigationViewModel()
        {
            MainMenu = new ObservableCollection<MenuItems>();


            MainMenu.Add(new MenuItems {MenuName = "New"});
            MainMenu[0].subMenu = new ObservableCollection<SubMenuItems>();
            MainMenu[0].subMenu.Add(new SubMenuItems { SubMenuName = "ABC" });
            MainMenu[0].subMenu.Add(new SubMenuItems { SubMenuName = "DEF" });
            MainMenu.Add(new MenuItems { MenuName = "Random"});
            MainMenu[1].subMenu = new ObservableCollection<SubMenuItems>();
            MainMenu[1].subMenu.Add(new SubMenuItems { SubMenuName = "PQR" });
            MainMenu[1].subMenu.Add(new SubMenuItems {SubMenuName = "UVW" });
            MainMenu[1].subMenu.Add(new SubMenuItems { SubMenuName = "XYZ" });
         }
    }
MainWindow.xaml
    <Grid.Resources>
                    <Style x:Key="MyStyle" TargetType="ListViewItem">
                        <Setter Property="ContentTemplate">

                            <Setter.Value>
                                <HierarchicalDataTemplate>
                                    <StackPanel x:Name="subMenuPanel" HorizontalAlignment="Stretch"
    Margin="0" Height="40" Width="{Binding Width, ElementName=gridMenu}">    
                                        <Button x:Name="btnMenu" Height="40" Width ="{Binding Width,
    ElementName=gridMenu}" FontSize="25" Margin="0" HorizontalAlignment="Stretch">
                                            <TextBlock HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch" Text="{Binding}"/>
                                        </Button>
                                        <ListView Background="{x:Null}" Height="auto" Width="auto" 
    ItemContainerStyle="{Binding MyStyle}"/>
                                    </StackPanel>

                                </HierarchicalDataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Grid.Resources>
                <ListView x:Name="MainMenuList" ItemsSource="{Binding MainMenu}" 
    ItemContainerStyle="{StaticResource MyStyle}" />

In XAML I have bound the Main listview with MainMenu observable collection. I now need to find out if MAinMenu further contains any Observable collection and if it does bind it to first listviewitem of the main listview
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...