使用按钮 wpf 创建自定义菜单

Create custom menu with a button wpf

这是我的自定义标题栏:

那个三点按钮是一个按钮控件。我想创建一个这样的菜单(用油漆画的!):

但我不知道。我知道有一个 "Menu" 控件,但我无法按需使用它。

"More" 按钮代码:

<Button x:Name="More" Grid.Column="1" Style="{DynamicResource TitleBarButton}" IsTabStop="False" Focusable="False">
    <Button.Background>
        <ImageBrush ImageSource="icons/More.png"/>
    </Button.Background>
</Button>

您可以将 Button 替换为 ToggleButton 并使用绑定到 ToggleButtonIsChecked 属性 的 Popup ]:

<ToggleButton x:Name="More" Grid.Column="1" Style="{DynamicResource TitleBarButton}" IsTabStop="False" Focusable="False">
    <ToggleButton.Background>
        <ImageBrush ImageSource="icons/More.png"/>
    </ToggleButton.Background>
</ToggleButton>
<Popup IsOpen="{Binding IsChecked, ElementName=More}"
               PlacementTarget="{Binding ElementName=More}"
               Placement="Bottom">
    <StackPanel Width="100" Background="Yellow">
        <TextBlock Text="Check For Updates" />
        <Grid Background="DarkGray" Height="3" />
        <TextBlock Text="Do something else" />
    </StackPanel>
</Popup>

您当然可以将 Popup 的内容替换为您想要的任何内容。

enter image description here

您可能需要将设置菜单项用作按钮 (更多 WPF 按钮功能 int this book

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="40" />
        <RowDefinition Height="40" />

    </Grid.RowDefinitions>

    <Grid Grid.Row="0" Background="Green">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1.0*" />
            <ColumnDefinition Width="40" />
            <ColumnDefinition Width="40" />
            <ColumnDefinition Width="40" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="1" >
            <Menu Height="40" Width="40" Background="Green" >
                <MenuItem >

                    <!-- MENU HEADER -->
                    <MenuItem.Header>
                        <Border>
                            <Image Source="/WpfMenuButton;component/Images/DotsImg.PNG" />
                        </Border>

                    </MenuItem.Header>

                    <!-- MENU ITEM UPDATE -->
                    <MenuItem Click="CheckUpdate_Click" Background="Chocolate">

                        <MenuItem.Header>
                            <TextBlock Text="Check for update" VerticalAlignment="Center" HorizontalAlignment="Center" 
                            Height="30" Width="100" />
                        </MenuItem.Header>

                    </MenuItem>

                    <!-- MENU ITEM WIFI -->
                    <MenuItem Click="DoSomethingElse_Click" Background="LightGreen">
                        <MenuItem.Header>
                            <TextBlock Text="Do something else" VerticalAlignment="Center"
                                HorizontalAlignment="Center" Height="30" Width="140" />
                        </MenuItem.Header>
                    </MenuItem>

                    <!-- MENU ITEM ABOUT -->
                    <MenuItem Click="MenuAbout_Click" Background= "LightBlue">

                        <MenuItem.Header>
                            <TextBlock Text="About" VerticalAlignment="Center" HorizontalAlignment="Center" 
                                Height="30" Width="140" />
                        </MenuItem.Header>

                    </MenuItem>

                </MenuItem>
            </Menu>

        </Grid>
        <Grid Grid.Column="2" >
            <Button Content="-" Foreground="White" FontSize="28" Background="Green"/>
        </Grid>
        <Grid Grid.Column="3" >
            <Button Content="X" Foreground="White" FontSize="28" Background="Green"/>
        </Grid>

    </Grid>

</Grid>