我需要菜单子菜单在点击时保持打开,我使用我的menuitems的数据模板,而itemssource是自定义对象的列表,我也使用menuitem的控件模板,问题是当点击一个项目时在,菜单关闭,这是menuitem的代码:
<MenuItem HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="64" Height="{Binding ElementName=menu,Path=ActualHeight}"
HeaderTemplate="{DynamicResource wifiIcon}" HorizontalAlignment="Stretch" Name="wifiMenuItem"
StaysOpenOnClick="True" ItemTemplate="{StaticResource WifiNetworkDataTemplate}">
<Border BorderThickness="1" BorderBrush="Black"></Border>
</MenuItem>
datatemplate代码:
<DataTemplate x:Key="WifiNetworkDataTemplate" DataType="{x:Type model:WifiNetwork}">
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="0,10,0,0" Background="Transparent" x:Name="wifiStack">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Viewbox Grid.Column="0" x:Name="check" Width="16" Height="16" Visibility="{Binding IsConnected, Converter={StaticResource BooleanToVisibilityConverter}}" >
<Canvas Width="24" Height="24">
<Path Data="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" Fill="Blue" />
</Canvas>
</Viewbox>
<Viewbox Grid.Column="3" x:Name="wifiSignal" Width="16" Height="16" Margin="10,0,0,0" >
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x:Name="icon_vm_wifi" Width="32" Height="32">
<Path Fill="White" Data="M16.030 10.038c-4.179 0-8.082 1.662-10.992 4.678-0.207 0.216-0.201 0.557 0.015 0.764 0.104 0.101 0.239 0.152 0.374 0.152 0.141 0 0.283-0.055 0.389-0.165 2.706-2.802 6.333-4.348 10.214-4.348 3.809 0 7.388 1.495 10.077 4.208 0.208 0.21 0.553 0.214 0.765 0.004 0.211-0.211 0.213-0.555 0.004-0.765-2.894-2.922-6.745-4.529-10.846-4.529z" />
<Path Fill="White" Data="M29.821 11.573c-3.796-3.409-8.695-5.287-13.791-5.287-5.127 0-10.047 1.897-13.852 5.343-0.222 0.201-0.239 0.543-0.038 0.765 0.108 0.117 0.253 0.178 0.401 0.178 0.129 0 0.26-0.046 0.363-0.139 3.604-3.265 8.266-5.064 13.125-5.064 4.829 0 9.471 1.779 13.068 5.010 0.222 0.202 0.564 0.184 0.763-0.041 0.201-0.222 0.182-0.563-0.040-0.764z" />
<Path Fill="White" Data="M16.030 13.425c-3.19 0-6.183 1.59-8.009 4.253-0.169 0.246-0.108 0.582 0.139 0.751 0.093 0.066 0.201 0.095 0.306 0.095 0.171 0 0.34-0.082 0.446-0.235 1.625-2.367 4.284-3.783 7.117-3.783 2.82 0 5.476 1.405 7.1 3.758 0.171 0.246 0.507 0.307 0.752 0.137 0.247-0.17 0.308-0.507 0.137-0.753-1.827-2.645-4.812-4.224-7.99-4.224z" />
<Path Fill="White" Data="M16.030 17.987c-2.129 0-3.862 1.734-3.862 3.863 0 2.129 1.732 3.864 3.862 3.864s3.864-1.734 3.864-3.864c0-2.128-1.734-3.863-3.864-3.863zM16.030 24.632c-1.534 0-2.78-1.248-2.78-2.782s1.246-2.781 2.78-2.781 2.782 1.247 2.782 2.781c0 1.534-1.248 2.782-2.782 2.782z" />
</Canvas>
</Viewbox>
<Viewbox Grid.Column="2" x:Name="passwordNeeded" Width="16" Height="16" Margin="10,0,0,0" Visibility="{Binding IsSecure, Converter={StaticResource BooleanToVisibilityConverter}}" >
<Canvas Width="24" Height="24">
<Path Data="M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z" Fill="Black" />
</Canvas>
</Viewbox>
<TextBlock Grid.Column="1" TextAlignment="Left" x:Name="wifiName" Text="{Binding Name}" HorizontalAlignment="Left" ></TextBlock>
</Grid>
<TextBlock x:Name="invalidPasswordTextBlock" Visibility="Collapsed"/>
<PasswordBox x:Name="wifiPasswordBox" Visibility="{Binding PasswordBoxShown, Converter={StaticResource BooleanToVisibilityConverter}}" />
</StackPanel>
</DataTemplate>
menuitem控件模板的代码:
<!--Menu Item-->
<ControlTemplate x:Key="menuItem1" TargetType="{x:Type MenuItem}">
<Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<Grid>
<Border x:Name="GlyphPanel" Background="Transparent"/>
<ContentPresenter Content="{TemplateBinding Header}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"
ContentTemplate="{TemplateBinding HeaderTemplate}" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
/>
<Popup x:Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsSubmenuOpen}"
AllowsTransparency="True"
Focusable="True"
StaysOpen="{TemplateBinding StaysOpenOnClick}"
PopupAnimation="Fade">
<Border Padding="10" Background="White" Opacity="0.7">
<ItemsPresenter HorizontalAlignment="Center" />
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter TargetName="templateRoot" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
<Setter Property="Template" Value="{StaticResource menuItem1}"/>
</Style>
答案 0 :(得分:0)
通过在wifimenuitem中使用另一个menuitem并设置其StaysOpenOnClick =“True”并使用列表框并在代码中设置其itemtemplate和itemssource来解决它:
<MenuItem StaysOpenOnClick="True">
<MenuItem.Header>
<ListBox x:Name="myListbox" ItemTemplate="{StaticResource WifiNetworkDataTemplate}" Focusable="True"></ListBox>
</MenuItem.Header>
</MenuItem>