Почему это DropdownMenu не работает в WPF - PullRequest
0 голосов
/ 27 апреля 2020

Я пытаюсь сделать DropdownMenu в WPF с кодом в VB. По какой-то причине я не могу понять, что DropdownMenu не работает так, как мне хотелось бы.

Это файл MainWindow.xaml:

<Window x:Class="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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    mc:Ignorable="d" Height="600" Width="1080" Foreground="White" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="250"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <materialDesign:ColorZone Mode="PrimaryMid" Grid.ColumnSpan="2" HorizontalAlignment="Stretch">
        <Grid>
            <materialDesign:PopupBox PlacementMode="BottomAndAlignRightEdges" HorizontalAlignment="Right" Margin="10"/>
        </Grid>
    </materialDesign:ColorZone>
    <Grid HorizontalAlignment="Stretch" Grid.Row="1" Background="{StaticResource PrimaryHueMidBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="70"/>
            <RowDefinition Height="326*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Background="GhostWhite">
            <Image Source="Assets/logo.png"/>
        </Grid>
        <ScrollViewer HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Grid.Row="1">
            <StackPanel x:Name="Menu" Margin="10"/>
        </ScrollViewer>
    </Grid>
</Grid>
</Window>

Это MainWindow.xaml. VB-файл:

Imports MaterialDesignThemes.Wpf
Imports Project.ViewModel


Partial Public Class MainWindow
Inherits Window

Public Sub New()
    InitializeComponent()
    Dim menuRegister = New List(Of SubItem)()
    menuRegister.Add(New SubItem("Customer"))
    menuRegister.Add(New SubItem("Providers"))
    menuRegister.Add(New SubItem("Employees"))
    menuRegister.Add(New SubItem("Products"))
    Dim item6 = New ItemMenu("Register", menuRegister, PackIconKind.Register)
    Dim menuSchedule = New List(Of SubItem)()
    menuSchedule.Add(New SubItem("Services"))
    menuSchedule.Add(New SubItem("Meetings"))
    Dim item1 = New ItemMenu("Appointments", menuSchedule, PackIconKind.Schedule)
    Dim menuReports = New List(Of SubItem)()
    menuReports.Add(New SubItem("Customers"))
    menuReports.Add(New SubItem("Providers"))
    menuReports.Add(New SubItem("Products"))
    menuReports.Add(New SubItem("Stock"))
    menuReports.Add(New SubItem("Sales"))
    Dim item2 = New ItemMenu("Reports", menuReports, PackIconKind.FileReport)
    Dim menuExpenses = New List(Of SubItem)()
    menuExpenses.Add(New SubItem("Fixed"))
    menuExpenses.Add(New SubItem("Variable"))
    Dim item3 = New ItemMenu("Expenses", menuExpenses, PackIconKind.ShoppingBasket)
    Dim menuFinancial = New List(Of SubItem)()
    menuFinancial.Add(New SubItem("Cash flow"))
    Dim item4 = New ItemMenu("Financial", menuFinancial, PackIconKind.ScaleBalance)
    Dim item0 = New ItemMenu("Dashboard", New UserControl(), PackIconKind.ViewDashboard)
    Menu.Children.Add(New UserControlMenuItem(item0))
    Menu.Children.Add(New UserControlMenuItem(item6))
    Menu.Children.Add(New UserControlMenuItem(item1))
    Menu.Children.Add(New UserControlMenuItem(item2))
    Menu.Children.Add(New UserControlMenuItem(item3))
    Menu.Children.Add(New UserControlMenuItem(item4))
End Sub
End Class

Это класс ItemMenu:

Imports MaterialDesignThemes.Wpf

Namespace ViewModel
Public Class ItemMenu
    Public Sub New(ByVal header As String, ByVal subItems As List(Of SubItem), ByVal icon As PackIconKind)
        header = header
        subItems = subItems
        icon = icon
    End Sub

    Public Sub New(ByVal header As String, ByVal screen As UserControl, ByVal icon As PackIconKind)
        header = header
        screen = screen
        icon = icon
    End Sub

    Public Property Header As String
    Public Property Icon As PackIconKind
    Public Property SubItems As List(Of SubItem)
    Public Property Screen As UserControl
End Class
End Namespace

Это класс подэлемента:

Namespace ViewModel
Public Class SubItem
    Public Sub New(ByVal name As String, ByVal Optional screen As UserControl = Nothing)
        name = name
        screen = screen
    End Sub

    Public Property Name As String
    Public Property Screen As UserControl
End Class
End Namespace

Это файл UserControlMenuItem.xaml:

<UserControl x:Class="UserControlMenuItem"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         mc:Ignorable="d" >
<Grid>
    <materialDesign:PackIcon Kind="{Binding Path=Icon}" Width="15" Height="15" Margin="10 16" Foreground="White"/>
    <ListBoxItem x:Name="ListViewItemMenu" Content="{Binding Path=Header}" Padding="37 14" FontSize="15" Foreground="White"/>
    <Expander x:Name="ExpanderMenu" Header="{Binding Path=Header}" IsExpanded="False" Width="210" HorizontalAlignment="Right" Background="{x:Null}" Foreground="White">
        <ListView x:Name="ListViewMenu" ItemsSource="{Binding Path=SubItems}" Foreground="White" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" Padding="20 5"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Expander>
</Grid>

Это файл UserControlMenuItem.xaml.vb:

Imports Project.ViewModel

Partial Public Class UserControlMenuItem
Inherits UserControl

Public Sub New(ByVal itemMenu As ItemMenu)
    InitializeComponent()
    ExpanderMenu.Visibility = If(itemMenu.SubItems Is Nothing, Visibility.Collapsed, Visibility.Visible)
    ListViewItemMenu.Visibility = If(itemMenu.SubItems Is Nothing, Visibility.Visible, Visibility.Collapsed)
    Me.DataContext = itemMenu
End Sub

Конечный класс

Это окно, когда приложение работает: enter image description here

Что мне здесь не хватает?

Дизайн, клонированный из DropDownMenu

1 Ответ

0 голосов
/ 29 апреля 2020

Я решил эту проблему. Это была деталь, которую я не осознавал. Это исправленные классы:

Класс ItemMenu:

Imports MaterialDesignThemes.Wpf

Namespace ViewModel
Public Class ItemMenu
    Public Sub New(ByVal header As String, ByVal subItems As List(Of SubItem), ByVal icon As PackIconKind)
        Me.Header = header
        Me.SubItems = subItems
        Me.Icon = icon
    End Sub

    Public Sub New(ByVal header As String, ByVal screen As UserControl, ByVal icon As PackIconKind)
        Me.Header = header 
        Me.Screen = screen 
        Me.Icon = icon 
    End Sub

    Public Property Header As String
    Public Property Icon As PackIconKind
    Public Property SubItems As List(Of SubItem)
    Public Property Screen As UserControl
End Class
End Namespace

Класс SubItem:

Namespace ViewModel
Public Class SubItem
    Public Sub New(ByVal name As String, ByVal Optional screen As UserControl = Nothing)
        Me.Name = name
        Me.Screen = screen
    End Sub

    Public Property Name As String
    Public Property Screen As UserControl
End Class
End Namespace

Осталось сделать ссылку на класс с «Я» (self на других языках, например Python), а затем присваивает значение свойству:

Me.Header = header 'Example in ItemMenu Class
Me.Name = name     'Example in SubItem Class
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...