我在WPF下拉菜单中遇到问题,因为我想像这样对此菜单进行编码:
但是我正在努力解决这个问题,我只能做到这样的事情:
所以我想知道是否有人可以帮助我让它看起来更像第一个例子。
样式(XAML):
<Style x:Key="{x:Type ContextMenu}" TargetType="{x:Type ContextMenu}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontFamily" Value="Segoe UI Light"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Background="#ff2e2f33" Opacity="1" Width="190">
<StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True" Margin="0,5,0,0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
XAML:
<TextBlock HorizontalAlignment="Left" Margin="12,114,0,0" VerticalAlignment="Top" Width="156" Height="30" Background="#FF2E2F33" FontFamily="Segoe UI Semibold" FontSize="16" Foreground="White" Text="Kategorie" TextWrapping="Wrap"/>
<Button Height="30" Width="34" HorizontalAlignment="Left" Style="{DynamicResource ButtonStyle2}" Name="buttonCategory" VerticalAlignment="Top" Click="button1_Click" Margin="168,114,0,0" FontFamily="Segoe UI Symbol" FontSize="18.667" Foreground="White" >
<Button.Content>
<Path x:Name="BtnArrow" Margin="4" VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
</Button.Content>
<Button.ContextMenu>
<ContextMenu>
<CheckBox HorizontalAlignment="Left" VerticalAlignment="Top" Content="Category 1" Template="{DynamicResource CustomCheckBox}" />
<CheckBox HorizontalAlignment="Left" VerticalAlignment="Top" Content="Category 2" Template="{DynamicResource CustomCheckBox}" />
<CheckBox HorizontalAlignment="Left" VerticalAlignment="Top" Content="Category 3" Template="{DynamicResource CustomCheckBox}" />
<CheckBox HorizontalAlignment="Left" VerticalAlignment="Top" Content="Category 4" Template="{DynamicResource CustomCheckBox}" />
</ContextMenu>
</Button.ContextMenu>
</Button>
C#代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
(sender as Button).ContextMenu.IsEnabled = true;
(sender as Button).ContextMenu.PlacementTarget = (sender as Button);
(sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
(sender as Button).ContextMenu.IsOpen = true;
}