wpf - 为什么此 DropdownMenu 在 WPF 中不起作用
问题描述
我正在尝试使用 VB 中的代码隐藏在 WPF 中创建 DropdownMenu。由于任何原因,我无法意识到这个 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
结束类
我在这里缺少什么?
从DropDownMenu克隆的设计
解决方案
我解决了这个问题。这是一个我没有意识到的细节。这些是更正后的类:
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
只是用“Me”(self
在其他语言,如 Python)中引用类,然后为属性赋值:
Me.Header = header 'Example in ItemMenu Class
Me.Name = name 'Example in SubItem Class
推荐阅读
- azure - 通过 URL 参数链接到应用程序洞察搜索
- python - 如何使用 Django 处理上传的文件
- python - 摘自《Python Crash Course》一书,作者为什么要写'--snip--'?
- java - 程序生成的 Voronoi 道路
- java - 如何在 Mac 上的 IntelliJ 中安装 javafx?
- python - 为什么我可以通过引用键而不是值本身来更改 dict 值?
- python - Pandas - 使用列表中的字典规范化数据集
- ios - 导航控制器 segue 以模态方式呈现
- c# - 搜索让给线程池的 Task.Yield 等效项
- android - 显示在颤振应用程序中的图像帧上运行 tflite 的相机活动