2

ユーザーがボタンであるコントロールを「右クリック」したときに表示されるコンテキストメニューを作成したいと思います。残念ながら、一部のボタンは無効になっています。誰かが私を助けて、コンテキストメニューも与える方法を教えてもらえますか?

私の(機能していない)試してみてください:

        private void ShowRightClickMenu(object sender, MouseEventArgs e)
    {
        ContextMenu Temp = new ContextMenu();
        if (e.Button == MouseButtons.Right && secondTagObj[Convert.ToInt32(((Button)sender).Tag)].typ != string.Empty)
        {
            this.ContextMenu = Temp;        // works
            Temp.MenuItems.Add("Create.."); //works
            Temp.MenuItems.Add("Delete");   // works
        }
        if (raster[Convert.ToInt32(((Button)sender).Tag)].Enabled == false && e.Button == MouseButtons.Right)
        {
            this.ContextMenu = Temp;        // works not
            Temp.MenuItems.Add("New...");   // works not
        }
        else
        {
            this.ContextMenu = Temp;        // works, but only if button is visible
            Temp.MenuItems.Add("New...");   // works, but only if button is visible
        }
    }

よろしくお願いします。

4

2 に答える 2

1

WPFでは、を使用しContextMenuServiceて、無効になっているコントロールのコンテキストメニューを有効にできます。

private void ShowRightClickMenu(object sender, MouseEventArgs e)
{
    ContextMenu Temp = new ContextMenu();
    ContextMenuService.SetShowOnDisabled((Button)sender, true);
...

[読みやすくするための更新]WinFormsについては、Microsoftフォーラムでこのエントリを参照してください。

お役に立てれば。

于 2012-11-26T09:39:07.437 に答える
1

(@medasoclesの回答のコメントに示されているように、この質問がWPFに関するものであると想定しています...)

ContextMenuServiceWPFで@medasoclesによってほのめかされているように使用するには、次のようにします。

<Button Content="Blah" Command="{Binding MyCommand}" ContextMenuService.ShowOnDisabled="True">
    <Button.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Context Item" Command="{Binding MyContextCommand}" />
        </ContextMenu>
    </Button.ContextMenu>
</Button>

しかし、これで実際に問題が解決するとは思いません。ボタンを境界線のようなフレームに配置し、コンテキストメニューを境界線に追加する方が簡単だと思います。IsHitTestVisible次に、ボタンが無効になったときに設定するトリガーをボタンに追加しFalseます。これにより、インタラクションが背後の境界線に渡され、メニューが期待どおりに機能するようになります。

于 2019-11-18T21:52:40.747 に答える